使用matplotlib删除散点图中的点

2024-06-16 13:50:40 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码创建一个带白点的散点图。如何删除此点而不重新绘制整个图形?

g = Figure(figsize=(5,4), dpi=60);
b = g.add_subplot(111)
b.plot(x,y,'bo') # creates a blue dot
b.plot(x,y,'wo') # ovverrides the blue dot with a white dot (but the black circle around it remains)

Tags: the代码add图形plot绘制bluedot
1条回答
网友
1楼 · 发布于 2024-06-16 13:50:40

套印不等于去除。用第二个绘图调用绘制一个带黑色边框的白色标记。可以使用plot(x,y,'wo', mec='w')设置标记的edgecolor。

但如果您真的想删除它,请捕获返回的line对象,并调用其remove方法。

fig, ax = plt.subplots(subplot_kw={'xlim': [0,1],
                                   'ylim': [0,1]})


p1, = ax.plot(0.5, 0.5, 'bo') # creates a blue dot
p2, = ax.plot(0.5, 0.5, 'ro')

p2.remove()

上面的示例生成一个带有蓝色标记的图形。添加(前面)红色标记,但也会再次删除。

相关问题 更多 >