您遇到的问题是,文本边界框已展开以容纳完整的旋转文本,但是该框本身仍是在笛卡尔坐标中定义的。下图显示了两个文本,水平对齐为“左”,垂直对齐为“底”;问题是旋转后的文本的边界框边缘距离文本更远。
您想要的是使文本绕其自身周围的边缘点旋转,如下所示。
这可以通过使用
rotation_mode="anchor"参数来实现,该参数可
matplotlib.text.Text精确控制上述功能。
ax.text(..., rotation_mode="anchor")
在此示例中:
from matplotlib import pyplot as pltimport numpy as nplObjectsALLcnts = [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.9bottom = 50fig = 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()请注意,这使用给定的50个底部间距单位。您可以稍微增加此数字,以使条形图和文本之间具有更大的间距。
以下答案的初始版本有些过时。 我将其保留在此处以供参考。
您遇到的问题是,文本边界框已展开以容纳完整的旋转文本,但是该框本身仍是在笛卡尔坐标中定义的。下图显示了两个文本,水平对齐为“左”,垂直对齐为“底”;问题是旋转后的文本的边界框边缘距离文本更远。
一个简单的解决方案是将水平和垂直对齐方式定义为“中心”,这样文本的枢轴就保持不变,而与旋转无关。
然后的问题是要获得一个很好的估计文本的中心和条的顶部之间的距离。
一个人可以取文本中字母数量的一半,然后乘以某个系数。这需要通过反复试验才能找到。
bottom = 50rotations = 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)
您还可以尝试找出渲染文本的实际大小,并使用此信息找出坐标,
bottom = 50rotations = 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)
完整的复制代码:
import numpy as npimport matplotlib.pyplot as pltlObjectsALLcnts = [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.9bottom = 50fig = 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)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)plt.show()不幸的是,再次
2.7涉及到一些奇怪的因素。更不幸的是,在这种情况下,我完全不知道为什么它必须存在。但是结果可能仍然可以使用。
人们还可以使用以下问题的一种解决方案:相对于文本而不是边界框对齐任意旋转的文本注释



