我正在使用matplotlib.pyplot。
我想做以下几点:
我如何执行第4步?我想避免重新绘制背景点。
下面是一个缺少步骤4的代码示例。
import matplotlib.pyplot as plt
fig = plt.figure()
plt.xlim(-10,10)
plt.ylim(-10,10)
#step 1: background blue dot
plt.plot(0,0,marker='o',color='b')
#step 2: additional black dots
points_list = [(1,2),(3,4),(5,6)]
for point in points_list:
plt.plot(point[0],point[1],marker='o',color='k')
#step 3: save
plt.savefig('test.eps')
#step 4: remove additional black dots发布于 2018-07-24 13:28:33
plot函数返回表示所绘制数据的Line2D对象列表。这些对象有一个remove方法,它将从绘制它们的图形中删除它们(请注意,Line2D继承自Artist,您可以通过Line2D.__mro__进行检查):
remove() method of matplotlib.lines.Line2D instance
Remove the artist from the figure if possible. The effect
will not be visible until the figure is redrawn, e.g., with
:meth:`matplotlib.axes.Axes.draw_idle`. Call
:meth:`matplotlib.axes.Axes.relim` to update the axes limits
if desired.
[...]因此,您可以执行以下操作(我一次就绘制了单个点):
points = plt.plot(*zip(*points_list), 'o', color='k')[0]
# Remove the points (requires redrawing).
points.remove()保持for循环如下:
points = []
for point in points_list:
points.extend(
plt.plot(point[0], point[1], marker='o', color='k')
)
for p in points:
p.remove()或者更简洁地使用列表理解:
points = [plt.plot(*p, marker='o', color='k')[0] for p in points_list]发布于 2018-07-24 13:25:27
您可以通过这样做来删除所绘制的点:
temporaryPoints, = plt.plot(point[0],point[1],marker='o',color='k')
temporaryPoints.remove()发布于 2018-07-24 13:19:24
您可以使用:
#step 2
black_points, = plt.plot( zip(*points_list), marker="o", color="k")
#... step 3 ...
#...
#step 4
black_points.set_visible( False)https://stackoverflow.com/questions/51499605
复制相似问题