1

我是 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()

一切看起来都很好,但标签不在情节中?任何想法?

没有标签的图表???

4

1 回答 1

1

您缺少一个plt.legend(). 您只需将其添加到之前的任何位置plt.showfig1.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()

在此处输入图像描述

在此处查看有关如何使用图例的更多详细信息。

于 2015-11-27T10:00:32.290 回答