实际上,一种正统的方法是将图例完全分开。
您可以参考this link,或参考同一链接中的sn-p:
line1, = plt.plot([1, 2, 3], label="Line 1", linestyle='--')
line2, = plt.plot([3, 2, 1], label="Line 2", linewidth=4)
# Create a legend for the first line.
first_legend = plt.legend(handles=[line1], loc='upper right')
# Add the legend manually to the current Axes.
ax = plt.gca().add_artist(first_legend)
# Create another legend for the second line.
plt.legend(handles=[line2], loc='lower right')
plt.show()
您会发现轴上只存在一个传奇。这样做是为了可以重复调用 legend() 以将图例更新为轴上的最新句柄,因此要保留旧的图例实例,我们必须手动将它们添加到轴: