python - 如何在matplotlib中标记补丁

标签 python matplotlib plot label patch

我正在以交互方式在matplotlib中绘制矩形补丁。我想向每个补丁添加文本。我不想注释它们,因为它会降低速度。我正在使用补丁的'label'属性,但无法正常工作。 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()

最佳答案

您已经知道补丁的位置,因此您可以计算中心位置,并在其中添加一些文本:

import matplotlib.pyplot as plt
import matplotlib.patches as patches

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'))

centerx = centery = x + 0.5/2 # obviously use a different formula for different shapes

plt.text(centerx, centery,'lalala')
plt.show()

centeredtext
plt.text的坐标确定了文本的开始位置,因此您可以在x方向上微调一下,以使文本更加居中,例如centerx - 0.05。显然,@ JoeKington的建议是实现此目标的正确方法

关于python - 如何在matplotlib中标记补丁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32785705/

相关文章:

python - 两种不同的聚类方法(通过频谱分析)和两种不同的结果......发生了什么?

python - 有没有一种方法可以让我从 CSV 文件中绘制数据,其中列中的每 10 个数据点都是同一图表中的不同线?

r - 对两点之间的核密度图进行着色。

excel - 在 Excel 散点图中使用文本作为水平标签

python - 为什么调用带有 -m 选项的模块会将 sys.path[0] 设置为空字符串?

python - 在python中创建一个随机矩阵

python - Seaborn factor plot hue 要求不为空 "cells"

python - 实时 matplotlib 图在循环中不起作用

python - 是否可以用python在平行对齐的轴上绘制多维点

python - 将类型为 float64 的 np.array 转换为类型为 uint8 的缩放值