从python中的图形中删除精确曲线

时间:2018-09-24 16:08:55

标签: python matplotlib

如果我执行以下操作

import matplotlib.pyplot as plt
list1=[1,2,3]
list2=[4,5,6]
fig1=plt.figure(1)
plt.plot(list1)
plt.plot(list2)
plt.show()

我将在同一图中绘制list1和list2。如果以后我决定只需要在图中绘制list1并且要删除list2的曲线,该怎么办? 如果可能,我希望不使用add_subplot或其他类似命令来执行此操作。我假设两条曲线的xlim和ylim相同。

1 个答案:

答案 0 :(得分:2)

有三种可能性:

方法1 -隐藏曲线(但保留数据)

import matplotlib.pyplot as plt
list1=[1,2,3]
list2=[4,5,6]
fig1=plt.figure(1)
plot_list1 = plt.plot(list1)
plot_list2 = plt.plot(list2)
plt.setp(plot_list2,"visible",False) #hide the list2 curve
plt.show()

方法2 -删除数据

import matplotlib.pyplot as plt
list1=[1,2,3]
list2=[4,5,6]
fig1=plt.figure(1)
plot_list1 = plt.plot(list1)
plot_list2 = plt.plot(list2)
plt.setp(plot_list2,"data",([],[])) #remove list2 data
plt.show()

方法3 -删除曲线

import matplotlib.pyplot as plt
list1=[1,2,3]
list2=[4,5,6]
fig1=plt.figure(1)
plot_list1 = plt.plot(list1)
plot_list2 = plt.plot(list2)
plot_list2[0].remove()
plt.show()