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()
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()
5条答案
按热度按时间zmeyuzjn1#
几乎所有的绘图函数都会返回对
artist
对象的引用,该对象是通过如果您有引用,则可以通过
remove
(doc)函数删除艺术家,例如:lawou6xi2#
编辑:塔卡斯韦尔的答案比我的好
我把我的答案无论如何记录在案(因为赞成票很好:眨眼:)
如果不想显式保存所有行的引用,但知道要删除的行的索引,可以利用maptplotlib为您存储这些索引的事实。
是一个
matplotlib.lines.Line2D
的列表。因此,要删除第二条线,您可以h43kikqp3#
我也有同样的需求,对我来说,在数据序列中添加一个id,然后通过找到具有给定id的序列(集合)来删除它,这样做会更整洁。
zsbz8rwp4#
该代码生成欠阻尼二阶系统的阶跃响应。该代码还可用于说明曲线图的叠加。该代码生成并以图形方式显示两个时间常数参数值的响应。该代码还说明了在for循环中创建彗星。
wecizke35#
您也可以将其用于多个子地块