Python 带标签参数的楔块,但结果中没有标签

Python 带标签参数的楔块,但结果中没有标签,python,matplotlib,wedge,Python,Matplotlib,Wedge,我是python(3.4)和matplotlib的初学者。我想用以下代码创建一个楔子: 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, f

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

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()
一切看起来都很好,但标签不在绘图中?有什么想法吗


您缺少一个
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()
导入matplotlib.pyplot作为plt
将matplotlib.patches导入为修补程序
图1=plt.图()
ax1=fig1.add_子批(111,aspect='equal')
ax1.添加补片(补片.楔块(中心=(0,0),r=0.9,θ1=90,θ2=120,facecolor=“red”,label=“Test”))
plt.xlim(-1,1)
plt.ylim(-1,1)

plt.legend()#谢谢。很简单,但你必须知道。@Geosucher是的,的确如此。我建议你(如果你感兴趣的话)去看一下这本书。它们是一系列的课堂讲稿,其中概述了使用matplotlib所需的所有内容(在本例中,它们有更多课堂讲稿)。此外,请考虑支持/接受答案,以防它对你有所帮助。