当我执行以下代码时,我可以看到Demo
在水平顶部打印。 Demo2
使用dotted line
在图内打印。但是我看不到Demo1
吗?
为什么Demo1
不打印,为什么Demo
水平打印?
import matplotlib.pyplot as plt
x = [1,2,3,4,5,-1-2]
y = [-1,5, 100, -2, 50, 100]
t = [100, 110, 120, 130, 140, 150]
plt.figure()
ax1 = plt.subplot(3, 1, 1)
plt.title('Demo');
ax1.plot(t,x, 'b.:', label="Demo") # Showing top Horizontal
ax2 = plt.subplot(3, 1, 2, sharex=ax1)
ax2.plot(t,y, 'b.:', label="Demo1") # Not showing up
ax3 = plt.subplot(3, 1, 3, sharex=ax1)
ax3.plot(t,t, 'b.:', label="Demo2") # This is perfect how I wanted
plt.legend()
plt.show()
答案 0 :(得分:1)
您可以改用plt.figlegend()
,这样将为您提供输出
或在每个绘图后调用plt.legend()
,将图例分别添加到每个子绘图中,
import matplotlib.pyplot as plt
x = [1,2,3,4,5,-1-2]
y = [-1,5, 100, -2, 50, 100]
t = [100, 110, 120, 130, 140, 150]
plt.figure()
ax1 = plt.subplot(3, 1, 1)
plt.title('Demo');
ax1.plot(t,x, 'b.:', label="Demo") # Showing top Horizontal
plt.legend()
ax2 = plt.subplot(3, 1, 2, sharex=ax1)
ax2.plot(t,y, 'b.:', label="Demo1") # Not showing up
plt.legend()
ax3 = plt.subplot(3, 1, 3, sharex=ax1)
ax3.plot(t,t, 'b.:', label="Demo2") # This is perfect how I wanted
plt.legend()
plt.show()
如果这不是您想要的,this question的答案中还列出了其他方法