Python Matplotlib - 如何删除特定的直线或曲线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19569052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Matplotlib - How to remove a specific line or curve
提问by Jomme
I want to remove a specific line in a plot of multiple lines. Bellow is a given example which is not sufficient for me because it removes only the last plotted line and not the line that I want to remove. How can I do that? How can I address a specific line(by name, by number, by reference) throughout the program and delete that line?
我想删除多行图中的特定行。Bellow 是一个给定的示例,这对我来说还不够,因为它只删除了最后一条绘制的线,而不是我想要删除的线。我怎样才能做到这一点?如何在整个程序中寻址特定行(按名称、按编号、按引用)并删除该行?
self.axes.lines.remove(self.axes.lines[0])
采纳答案by tacaswell
Almost all of the plotting functions return a reference to the artist
object created ex:
几乎所有的绘图函数都返回对artist
创建的对象的引用,例如:
ln, = plot(x, y) # plot actually returns a list of artists, hence the ,
im = imshow(Z)
If you have the reference you can remove an artist via the remove
(doc)function ex:
如果您有参考资料,您可以通过remove
(doc)功能例如删除艺术家:
ln.remove()
im.remove()
回答by Francesco Montesano
If you don't want to save the reference of all the lines explicitly but you know the index of the line that you want to remove, you can use the fact that maptplotlib stores them for you.
如果您不想显式保存所有行的引用,但您知道要删除的行的索引,则可以使用 maptplotlib 为您存储它们的事实。
self.axes.lines
is a list of matplotlib.lines.Line2D
. So to remove, e.g., the second line drawn you can do
是一个列表matplotlib.lines.Line2D
。所以要删除,例如,绘制的第二条线,你可以做
self.axes.lines[1].remove()
回答by Andrew
I had the same need and for me, it turned out to be tidier to add an id to the data series and remove it later by finding the series (collection) with the given id.
我有同样的需求,对我来说,结果证明将 id 添加到数据系列并稍后通过查找具有给定 id 的系列(集合)将其删除会更整洁。
def add_series(x, id):
plt.plot(x, gid = id)
def remove_series(id):
for c in plt.collections: # possibly better to use: for c in plt.lines (see comment)
if c.get_gid() == id:
c.remove()
回答by Aravinda Murthy
The code generates step response for underdamped second order system. The code can also be used to illustrate overlaying of plots. The code generates and displays graphically, the response for two values of time constant parameter. The code also illustrates creation of comet in a for loop.
该代码为欠阻尼二阶系统生成阶跃响应。该代码还可用于说明图的叠加。该代码生成并以图形方式显示两个时间常数参数值的响应。该代码还说明了在 for 循环中创建 Comet。
import numpy as np
import matplotlib.pyplot as plt
The following programme runs on version 3.6.
Code generates a pair of lines and the line 2 is removed in a for loop which
simulates a comet effect
pts=100
t2 = np.linspace(0.0,5.0,pts)
t2=(t2/50)
tm=t2*(10**3)
nz=t2.size
tc=np.linspace(0.8,2.5,2)
nz=tc.size
for n in range (nz):
print(tc[n])
resp = 1 - np.exp(-tc[n]*tm*10**-3*50) * np.cos(2*np.pi*50*tm*10**-3)
for m in range(pts):
plt.xlim(0,100)
plt.ylim(0,2)
plt.xlabel('Time,in milliseconds',fontsize=12)
plt.ylabel('Respose',fontsize=12)
plt.title('Underdamped Second Order System Step Response',fontsize=14)
line1,=plt.plot(tm[0:m+1],resp[0:m+1],color='black',linewidth=0.2)
line2,=plt.plot(tm[m],resp[m],marker='o',color='red',markersize=5)
ax = plt.gca()
plt.pause(0.02)
ax.lines.remove(line2)
plt.grid('on')
plt.show()
回答by lazyalways
You can also use this for multiple subplots
您也可以将其用于多个子图
subfig, subax = plt.subplots(3)
def add_series(x, y0, y1, y2, gid):
plt.figure(subfig.number)
ln, = subax[0].plot(x, y0, gid=gid)
ln, = subax[1].plot(x, y1, gid=gid)
ln, = subax[2].plot(x, y2, gid=gid)
plt.draw()
def remove_series(self, gid):
plt.figure(subfig.number)
for c0, c1, c2 in zip(subax[0].lines, subax[1].lines, subax[2].lines):
if c0.get_gid() == gid:
c0.remove()
if c1.get_gid() == gid:
c1.remove()
if c2.get_gid() == gid:
c2.remove()
plt.draw()