使用 matplotlib 删除散点图中的一个点 [英] Removing a dot in a scatter plot with matplotlib
问题描述
下面的代码创建了一个带有白点的散点图.如何在不重绘整个图形的情况下删除该点?
The below code creates a scatter plot with a white dot. How can I remove this dot without redrawing the whole figure?
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)
推荐答案
过度绘图与删除不同.在第二个绘图调用中,您绘制一个带有黑色边框的白色标记.您可以使用 plot(x,y,'wo', mec='w')
设置标记的边缘颜色.
Overplotting is not the same as removing. With your second plot call you draw a white marker, with a black border. You can set the edgecolor for a marker with plot(x,y,'wo', mec='w')
.
但是如果你真的想移除它,捕获返回的线对象,并调用它的remove方法.
But if you really want to remove it, capture the returned line object, and call its remove method.
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()
上面的示例生成带有蓝色标记的图形.添加了一个红色标记(在前面),但又被删除了.
The example above results in a figure with a blue marker. A red marker is added (in front) but also removed again.
这篇关于使用 matplotlib 删除散点图中的一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!