从创建图例句柄fill_between
打电话,或者只是任何其他PolyCollection
您可以使用此 PolyCollection 并将其提供给图例。
import matplotlib.pyplot as plt
h = plt.fill_between([1,2,3],[1,3,4], y2=[1,2,3], color="#c1009b")
plt.legend(handles=[h], labels=["MyLabel"])
plt.show()
更简单的方法是直接使用label
的论点fill_between
用于创建自动图例条目的图(就像任何其他图一样)。
import matplotlib.pyplot as plt
plt.fill_between([0,1,2],[4,3,4], y2=[3,2.5,3], color="#c1009b", label="Label1")
plt.fill_between([0,1,1.3],[1,2,0.5], y2=[0,-1,0], color="#005ec1", label="Label2")
plt.fill_between([2,3,4],[0.5,1,0], y2=[-1,-1,-1.5], color="#005ec1", label="_noLabel")
plt.legend()
plt.show()