gpt4 book ai didi

python - 如何删除matplotlib、Python中以前的绘图?

转载 作者:行者123 更新时间:2023-11-30 23:34:29 31 4
gpt4 key购买 nike

我运行了 3 个 Python 脚本,每个脚本都在图中生成了一条曲线。

每条曲线都由数百条小线段组成。

因此,每条曲线都是由一系列 plot() 绘制的,而不是一个。

但是所有这些plot()共享相同的参数(例如,一条曲线的颜色、样式是一致的)。

因此,我认为仍然可以轻松删除特定脚本绘制的所有段。

我发现最近运行的脚本生成的曲线是错误的。因此,我希望删除它。但与此同时,我不能只是关闭窗口并重新绘制所有内容。我希望保留所有其他曲线

我该怎么做?

更新:绘制代码

for i, position in enumerate(positions):
if i == 0:
plt.plot([0,0], [0,0], color=COLOR, label=LABEL)
else:
plt.plot([positions[i - 1][0], position[0]], [positions[i - 1][1], position[1]], STYLE, color=COLOR)

#plt.plot([min(np.array(positions)[:,0]), max(np.array(positions)[:,0])], [0,0], color='k', label='East') # West-East
#plt.plot([0,0], [min(np.array(positions)[:,1]), max(np.array(positions)[:,1])], color='k', label='North') # South-North

plt.gca().set_aspect('equal', adjustable='box')

plt.title('Circle Through the Lobby 3 times', fontsize=18)
plt.xlabel('x (m)', fontsize=16)
plt.ylabel('y (m)', fontsize=16)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.draw()

最佳答案

我认为你的整个循环可以替换为:

pos = np.vstack(positions) # turn your Nx2 nested list -> Nx2 np.ndarray
x, y = pos.T # take the transpose so 2xN then unpack into x and y
ln, = plt.plot(x, y, STYLE, color=COLOR, label=LABEL)

请注意 它很重要,它会解压 plot 返回的列表。

如果你想删除这一行,只需这样做

ln.remove()  # remove the artist
plt.draw() # force a re-rendering of the canvas (figure) to reflect removal

我无法判断您使用 positions[-1] 是否是有意的,但如果您想强制它是周期性的,请这样做

pos = np.vstack(positions + positions[:1])

如果您确实想将每个线段绘制为单独的线,请使用 LineCollection,请参阅 https://stackoverflow.com/a/17241345/380231举个例子

关于python - 如何删除matplotlib、Python中以前的绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18075721/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com