for m=1:size(Y,2) plot1(m)=plot(x,Y(:,m),'Parent',axes1); set(plot1(m),'DisplayName',Legend_name{m},'LineWidth',Line_width,'Marker','s','MarkerSize',Marker_Size); end
时间: 2023-05-13 14:06:20 浏览: 36
这是一段 MATLAB 代码,用于绘制多条曲线。其中,Y 是一个矩阵,每一列代表一条曲线的数据;x 是横坐标的数据;Legend_name 是一个字符串数组,存储每条曲线的名称;Line_width 是曲线的线宽;Marker 和 Marker_Size 是曲线上数据点的标记和大小。
相关问题
import matplotlib.pyplot as plt from queue import Queue s = (0, 0, 0) # (人所在位置, 距起点距离, 已经走的步数) t = (4, 4, -1) # 终点为平台(4,4) movable = [(1, 0), (0, 1), (-1, 0), (0, -1)] step = 6 lake = [[1,1,1,1,1,1], [1,0,0,0,0,1], [1,0,1,1,0,1], [1,0,0,1,0,1], [1,0,1,0,0,1], [1,1,1,1,1,1]] def bfs(start, end): queue = Queue() queue.put(start) visited = set() visited.add(start) while not queue.empty(): curr = queue.get() if curr == end: return curr for i, j in movable: x0, y0, z0 = curr x, y = x0 + i, y0 + j if 0 <= x < 6 and 0 <= y < 6 and lake[x][y] == 0: if z0 + 1 <= step: next_state = (x, y, z0 + 1) if next_state not in visited: visited.add(next_state) queue.put(next_state) parent[next_state] = curr return None def find_path(parent, end): path = [] while end: path.append(end) end = parent.get(end) path.reverse() return path parent = {} end_state = bfs(s, t) if end_state: path = find_path(parent, end_state) for x, y, _ in path: lake[x][y] = 2 fig, ax = plt.subplots(figsize=(8, 8)) for row in range(6): for col in range(6): if lake[row][col] == 0: ax.plot(row, col, '.', color='white', markersize=35) elif lake[row][col] == 1: ax.plot(row, col, '#', color='brown', markersize=35) elif lake[row][col] == 2: ax.plot(row, col, '*', color='blue', markersize=35) ax.plot(row, col, color='black', linestyle='-', linewidth=2) ax.set_xticks([]) ax.set_yticks([]) ax.set_xlim([-0.5, 5.5]) ax.set_ylim([-0.5, 5.5]) plt.show()修正这个代码
在这段代码中,有一些问题。首先,你的导入语句和第一行代码应该分开写。其次,`parent`变量在`find_path`函数中未定义,需要传递给该函数。最后,你需要将`col`的值在循环内部更新,而不是在循环外部。下面是修正后的代码:
```
import matplotlib.pyplot as plt
from queue import Queue
s = (0, 0, 0) # (人所在位置, 距起点距离, 已经走的步数)
t = (4, 4, -1) # 终点为平台(4,4)
movable = [(1, 0), (0, 1), (-1, 0), (0, -1)]
step = 6
lake = [[1,1,1,1,1,1],
[1,0,0,0,0,1],
[1,0,1,1,0,1],
[1,0,0,1,0,1],
[1,0,1,0,0,1],
[1,1,1,1,1,1]]
def bfs(start, end):
queue = Queue()
queue.put(start)
visited = set()
visited.add(start)
parent = {}
while not queue.empty():
curr = queue.get()
if curr == end:
return curr
for i, j in movable:
x0, y0, z0 = curr
x, y = x0 + i, y0 + j
if 0 <= x < 6 and 0 <= y < 6 and lake[x][y] == 0:
if z0 + 1 <= step:
next_state = (x, y, z0 + 1)
if next_state not in visited:
visited.add(next_state)
queue.put(next_state)
parent[next_state] = curr
return None
def find_path(parent, end):
path = []
while end:
path.append(end)
end = parent.get(end)
path.reverse()
return path
parent = {}
end_state = bfs(s, t)
if end_state:
path = find_path(parent, end_state)
for row in range(6):
for col in range(6):
if lake[row][col] == 0:
plt.plot(row, col, '.', color='white', markersize=35)
elif lake[row][col] == 1:
plt.plot(row, col, '#', color='brown', markersize=35)
elif lake[row][col] == 2:
plt.plot(row, col, '*', color='blue', markersize=35)
plt.plot(row, col, color='black', linestyle='-', linewidth=2)
col = 0
plt.xticks([])
plt.yticks([])
plt.xlim([-0.5, 5.5])
plt.ylim([-0.5, 5.5])
plt.show()
```
这个修正后的代码将绘制一个可行路径的图形。
MyChartView::MyChartView(QChart* chart, QWidget* parent) : MyChartView( parent ) { setChart(chart); setMouseTracking(true); // 创建用于显示游标线的QGraphicsLineItem int lineCount = chart->series().size(); // 获取曲线数 for (int i = 0; i < lineCount; ++i) { QGraphicsLineItem* cursorLine = new QGraphicsLineItem(chart->plotArea().x(), chart->plotArea().y(), chart->plotArea().x(), chart->plotArea().height()); cursorLine->setPen(QPen(QColor(Qt::blue), 1)); cursorLine->setVisible(true); chart->scene()->addItem(cursorLine); m_cursorLines.append(cursorLine); } }这样设置之后QGraphicsLineItem 不显示的原因是什么?该怎么让图层置顶显示呢
有可能是因为 `chart->plotArea().x()` 或 `chart->plotArea().y()` 返回的值为0,导致游标线在视图内不可见。您可以检查一下这两个值是否正确。
如果游标线已经被创建但是没有显示出来,您可以使用 `setZValue()` 函数将其放置在图层的最上面,例如:
```cpp
cursorLine->setZValue(10); // 10为游标线的图层编号,可以调整为其他数字
```
这样可以确保游标线始终处于视图的最上层。
阅读全文