带有label参数的Wedges,但结果中没有标签

时间:2015-11-27 09:46:59

标签: python matplotlib wedge

我是python(3.4)和matplotlib的初学者。我想用以下代码创建一个wedge:

import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(patches.Wedge(center=(0,0), r=0.9, theta1=90, theta2=120, facecolor="red", label="Test"))
plt.xlim(-1, 1)
plt.ylim(-1, 1)
fig1.savefig('wedge1.png', dpi=90, bbox_inches='tight')
plt.show()

一切看起来都不错,但标签不在剧情中?任何的想法?

Chart with no Label???

1 个答案:

答案 0 :(得分:1)

您错过了plt.legend()。您只需将其添加到plt.show之前的任何位置(如果您希望将其保存在图像中,也可以在fig1.savefig之前),并在所有图表之后添加:

import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(patches.Wedge(center=(0,0), r=0.9, theta1=90, theta2=120, facecolor="red", label="Test"))
plt.xlim(-1, 1)
plt.ylim(-1, 1)
plt.legend()  # <--- here
fig1.savefig('wedge1.png', dpi=90, bbox_inches='tight')
plt.show()

enter image description here

有关how to use legends的详细信息,请查看此处。