【发布时间】:2015-09-25 15:31:40
【问题描述】:
我在 matplotlib 中以交互模式绘制矩形补丁。我想为每个补丁添加文本。我不想注释它们,因为它会降低速度。我正在使用补丁的“标签”属性,但它不起作用。 Ayone 知道如何在补丁中添加 1 个字符串。
import matplotlib.pyplot as plt
import matplotlib.patches as patches
plt.ion()
plt.show()
x = y = 0.1
fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
patch = ax1.add_patch(patches.Rectangle((x, y), 0.5, 0.5,
alpha=0.1,facecolor='red',label='Label'))
plt.pause(0)
plt.close()
【问题讨论】:
-
如果要显示标签,需要调用
plt.legend()。 -
您可能想look at this example from the docs 它向您展示了如何在补丁中的各个点添加文本。如果您真的想在图例中显示所有现有标签 - 请参阅
plt.legend() -
补丁是任意的 Artist 对象,所以
plt.legend()不是你想要的。如果要在对象上显示可见文本,请使用plt.text()或plt.annotate()。在 matplotlib 中,label不是一个可见的文本标签,它是一个内部“句柄”,用于查找具有plt.findobj()的子类 Artist 的对象,但这似乎不适用于补丁。
标签: python matplotlib plot label patch