有时候需要绘制多段曲线,还需要绘制多次,可以给曲线绘图增加句柄,然后把句柄放到变量里面,然后对保存起来的句柄分别添加图例。
看图
看码
import matplotlib.pylab as plt
def get_v_value(t):
t2=t*t
return t2
if __name__ == '__main__':
cycllist=[2,3, 4]
pllist=[]
lgtextlist=[]
fig1=plt.figure()
colorlsit = ['red', 'salmon', 'slateblue', 'cyan', 'olive', 'purple', 'bisque']
icolor=0
for cycle in cycllist:
htlist=[[1,10],[11,20],[21,30]]
for thv in htlist:
tmp = [cycle, get_v_value(cycle)]
p1, = plt.plot([thv[0], thv[1]], tmp, color=colorlsit[icolor])
icolor = icolor + 1
labeltext = 'n=' + str(cycle)
pllist.append(p1)
lgtextlist.append(labeltext)
plt.legend(pllist, lgtextlist)
plt.show()