在 Matplotlib、Python3 中的极坐标/径向条形图中的条形图顶部获取标签

Sti*_*den 6 python matplotlib bar-chart python-3.x polar-coordinates

我想创建一个径向条形图。我有以下 Python3 代码:

lObjectsALLcnts = [1, 1, 1, 2, 2, 3, 5, 14, 15, 20, 32, 33, 51, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 6, 7, 7, 10, 10, 14, 14, 14, 17, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 5, 5, 6, 14, 14, 27, 27, 1, 1, 2, 3, 4, 4, 5]`

lObjectsALLlbls = ['DuctPipe', 'Column', 'Protrusion', 'Tree', 'Pole', 'Bar', 'Undefined', 'EarthingConductor', 'Grooves', 'UtilityPipe', 'Cables', 'RainPipe', 'Moulding', 'Intrusion', 'PowerPlug', 'UtilityBox', 'Balcony', 'Lighting', 'Lock', 'Doorbell', 'Alarm', 'LetterBox', 'Grate', 'Undefined', 'CableBox', 'Canopy', 'Vent', 'PowerBox', 'UtilityHole', 'Recess', 'Protrusion', 'Shutter', 'Handrail', 'Lock', 'Mirror', 'SecuritySpike', 'Bench', 'Intrusion', 'Picture', 'Showcase', 'Camera', 'Undefined', 'Stair', 'Protrusion', 'Alarm', 'Graffiti', 'Lighting', 'Ornaments', 'SecurityBar', 'Grate', 'Vent', 'Lighting', 'UtilityHole', 'Intrusion', 'Undefined', 'Protrusion']

iN = len(lObjectsALLcnts)
arrCnts = np.array(lObjectsALLcnts)

theta=np.arange(0,2*np.pi,2*np.pi/iN)
width = (2*np.pi)/iN *0.9

fig = plt.figure(figsize=(8, 8))
ax = fig.add_axes([0.1, 0.1, 0.75, 0.75], polar=True)
bars = ax.bar(theta, arrCnts, width=width, bottom=50)
ax.set_xticks(theta)
plt.axis('off')
Run Code Online (Sandbox Code Playgroud)

这将创建以下图像:

Radialbartchart_nolabels

创建此后,我想添加标签,但我在找到正确的坐标时遇到了一些麻烦。标签应沿条的方向旋转。

我想出的最好的方法是添加以下代码:

rotations = [np.degrees(i) for i in theta]
for i in rotations: i = int(i)
for x, bar, rotation, label in zip(theta, bars, rotations, lObjectsALLlbls):
     height = bar.get_height() + 50
     ax.text(x + bar.get_width()/2, height, label, ha='center', va='bottom', rotation=rotation)
Run Code Online (Sandbox Code Playgroud)

这将创建以下内容:

Radialbarchart_wlabels

有人可以帮我找到标签的正确坐标吗?我一直在寻找答案,例如在 matplotlib 条形图上添加值标签 并将其转换为极坐标条形图。但没有成功。

提前致谢,

StackOverflow 上的长期读者,但我第一次找不到答案。

Imp*_*est 6

您遇到的问题是文本边界框被扩展以容纳完整的旋转文本,但该框本身仍以笛卡尔坐标定义。下图显示了水平对齐“左”和垂直对齐“底部”的两个文本;问题是旋转文本的边界框边缘离文本更远。

在此处输入图片说明

您想要的是让文本围绕其自身周围的边缘点旋转,如下所示。

在此处输入图片说明

这可以使用 的rotation_mode="anchor"参数来实现matplotlib.text.Text,它精确地控制了上述功能。

ax.text(..., rotation_mode="anchor")
Run Code Online (Sandbox Code Playgroud)

在这个例子中:

from matplotlib import pyplot as plt
import numpy as np

lObjectsALLcnts = [1, 1, 1, 2, 2, 3, 5, 14, 15, 20, 32, 33, 51, 1, 1, 2, 2, 3, 3, 3, 3, 
                   3, 4, 6, 7, 7, 10, 10, 14, 14, 14, 17, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 
                   5, 5, 6, 14, 14, 27, 27, 1, 1, 2, 3, 4, 4, 5]

lObjectsALLlbls = ['DuctPipe', 'Column', 'Protrusion', 'Tree', 'Pole', 'Bar', 'Undefined', 
                   'EarthingConductor', 'Grooves', 'UtilityPipe', 'Cables', 'RainPipe', 'Moulding', 
                   'Intrusion', 'PowerPlug', 'UtilityBox', 'Balcony', 'Lighting', 'Lock', 'Doorbell', 
                   'Alarm', 'LetterBox', 'Grate', 'Undefined', 'CableBox', 'Canopy', 'Vent', 'PowerBox', 
                   'UtilityHole', 'Recess', 'Protrusion', 'Shutter', 'Handrail', 'Lock', 'Mirror', 
                   'SecuritySpike', 'Bench', 'Intrusion', 'Picture', 'Showcase', 'Camera', 
                   'Undefined', 'Stair', 'Protrusion', 'Alarm', 'Graffiti', 'Lighting', 'Ornaments', 
                   'SecurityBar', 
                   'Grate', 'Vent', 'Lighting', 'UtilityHole', 'Intrusion', 'Undefined', 'Protrusion']

iN = len(lObjectsALLcnts)
arrCnts = np.array(lObjectsALLcnts)

theta=np.arange(0,2*np.pi,2*np.pi/iN)
width = (2*np.pi)/iN *0.9
bottom = 50

fig = plt.figure(figsize=(8,8))
ax = fig.add_axes([0.1, 0.1, 0.75, 0.75], polar=True)
bars = ax.bar(theta, arrCnts, width=width, bottom=bottom)

plt.axis('off')

rotations = np.rad2deg(theta)
for x, bar, rotation, label in zip(theta, bars, rotations, lObjectsALLlbls):
    lab = ax.text(x,bottom+bar.get_height() , label, 
             ha='left', va='center', rotation=rotation, rotation_mode="anchor",)   
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

请注意,这使用给定的 50 个单位的底部间距。您可以稍微增加此数字以在条形和文本之间有更多间距。


这个答案的以下初始版本在某种程度上已经过时了。我会留在这里以供参考。

您遇到的问题是文本边界框被扩展以容纳完整的旋转文本,但该框本身仍以笛卡尔坐标定义。下图显示了水平对齐“左”和垂直对齐“底部”的两个文本;问题是旋转文本的边界框边缘离文本更远。

在此处输入图片说明

一个简单的解决方案可能是将水平和垂直对齐定义为“居中”,这样文本的枢轴保持不变而与其旋转无关。

在此处输入图片说明

那么问题将是对文本中心和条形顶部之间的距离进行很好的估计。

人们可以取文本中字母数量的一半,然后乘以某个因子。这需要通过反复试验才能找到。

bottom = 50
rotations = np.rad2deg(theta)
y0,y1 = ax.get_ylim()

for x, bar, rotation, label in zip(theta, bars, rotations, lObjectsALLlbls):
     offset = (bottom+bar.get_height())/(y1-y0)
     h =offset + len(label)/2.*0.032
     lab = ax.text(x, h, label, transform=ax.get_xaxis_transform(), 
             ha='center', va='center')
     lab.set_rotation(rotation)
Run Code Online (Sandbox Code Playgroud)

您还可以尝试找出渲染文本的实际大小并使用此信息找出坐标,

bottom = 50
rotations = np.rad2deg(theta)
y0,y1 = ax.get_ylim()

for x, bar, rotation, label in zip(theta, bars, rotations, lObjectsALLlbls):
     offset = (bottom+bar.get_height())/(y1-y0)
     lab = ax.text(0, 0, label, transform=None, 
             ha='center', va='center')
     renderer = ax.figure.canvas.get_renderer()
     bbox = lab.get_window_extent(renderer=renderer)
     invb = ax.transData.inverted().transform([[0,0],[bbox.width,0] ])
     lab.set_position((x,offset+(invb[1][0]-invb[0][0])/2.*2.7 ) )
     lab.set_transform(ax.get_xaxis_transform())
     lab.set_rotation(rotation)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

完整的复制代码:

ax.text(..., rotation_mode="anchor")
Run Code Online (Sandbox Code Playgroud)

不幸的是,这里又2.7涉及到一些奇怪的因素。更不幸的是,在这种情况下,我完全不知道为什么它必须在那里。但结果可能仍然足够好。

人们也可以使用这个问题的解决方案:对齐任意旋转的文本注释相对于文本,而不是边界框

  • 由于我的声誉不允许我投票:感谢您提供清晰的解决方案。正如您所建议的,在底部添加 1.25 可以解决栏和文本之间的间距问题。我还在 x 中添加了 -0.01,因为左上角的标签在条形侧面有点太多。为了完全公正,在更新的解决方案中,您可以删除 'y0,y1 = ax.get_ylim()'、'fig.canvas.draw()' 和 'offset = (bottom+bar.get_height())/(y1 -y0)'行。它们是您最初解决方案的剩余部分。 (2认同)