不更新图例。相反,你只是在你的情节之后创建它。
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
p1 = ax.fill_between([0, 1], 0, 1, color='green', label='foo')
p2 = ax.fill_between([1, 2], 0, 1, color='orange', label='bar')
ax.legend()
plt.show()
如果以后创建了一个情节(例如,通过交互使用或动画),您仍然可以调用
legend
再一次。
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
p1 = ax.fill_between([0, 1], 0, 1, color='green', label='foo')
ax.legend()
p2 = ax.fill_between([1, 2], 0, 1, color='orange', label='bar')
ax.legend()
plt.show()