Skip to main content
 首页 » 编程设计

python之如何将标签对齐到极坐标条形图的内边缘

2024年10月17日8傻小

我无法制作我想要的可视化效果。我还不能放图片,所以链接在下面。我几乎得到了我想要的。问题是标签放置不正确。

倒置极杆演示

我想让标签按原样旋转,但要让标签的右边缘刚好对齐到圆圈的外边缘内。

编辑澄清:
我在这个例子中使用的标签都是“测试”。对于实际数据,这些标签的长度会有所不同。我想移动标签的末端,以便它们的最后一个字母始终位于圆圈的外边缘旁边。所以在这种情况下,所有的“g”都会靠近外边缘。

import matplotlib.pyplot as mpl 
import numpy as np 
import random 
 
bgcolor = '#222222' 
barcolor = '#6699cc' 
bottom = 15 
 
N = 32 
Values = np.random.random(N)*10 
MetricLabels = ['testing' for _ in range(1, N+1)] 
 
# Select the radii, thetas, and widths. 
Radii = -5*np.ones(N)-Values 
Theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) 
width = 2*np.pi/N 
 
# Make a list of shifted thetas to place the labels at. 
ThetaShifted = np.copy(Theta) 
for i in range(N-1): 
    ThetaShifted[i] = (Theta[i] + Theta[i+1])/2.0 
ThetaShifted[-1] = (Theta[-1] + 2.0*np.pi)/2.0 
 
# Make the figure 
fig = mpl.figure() 
ax = fig.add_subplot(111, projection='polar') 
bars = ax.bar(Theta, Radii, width=width, bottom=bottom) 
 
# Set the outer ring to be invisible. 
ax.spines["polar"].set_visible(False) 
 
# Set the grid line locations but set the labels to be invisible. 
ax.grid(False) 
ax.set_thetagrids([], visible=False) 
ax.set_rgrids([3], visible=False) 
 
# Apply colors to bars based on the settings above. 
for v, bar in zip(Values, bars): 
        bar.set_facecolor(barcolor) 
        bar.set_edgecolor(bar.get_facecolor()) 
 
# Show the metric and value labels 
for counter in range(N): 
    ax.text(ThetaShifted[counter], bottom-3, MetricLabels[counter],  
            horizontalalignment='center', verticalalignment='baseline',  
            rotation=(counter+.5)*360/N, color=bgcolor) 
    ax.text(ThetaShifted[counter], bottom+0.75, np.round(Values[counter],2),  
            horizontalalignment='center', verticalalignment='center',  
            color=bars[counter].get_facecolor()) 
 
# Set the background color to be a dark grey, 
ax.set_axis_bgcolor(bgcolor) 
fig.set_facecolor(bgcolor) 
 
# Show the figure. 
mpl.show() 

请您参考如下方法:

我实际上解决了我的问题。请参见下面的图像和代码。解决它的主要方法是使用等宽字体系列并使用 rjust 创建标签字符串,使其从一开始就固定长度并右对齐。之后,只需为每个标签选择正确的径向位置,当它们的字符数都相同时,这应该会容易得多。

Initial demo solved

import matplotlib.pyplot as mpl 
import numpy as np 
import random 
 
bgcolor = '#222222' 
barcolor = '#6699cc' 
bottom = 15 
 
N = 32 
Values = np.random.random(N)*10 
MetricLabels = [('A'*(4+int(8*random.random()))).rjust(10) for _ in range(1, N+1)] 
 
# Select the radii, thetas, and widths. 
Radii = -5*np.ones(N)-Values 
Theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) 
width = 2*np.pi/N 
 
# Make a list of shifted thetas to place the labels at. 
ThetaShifted = np.copy(Theta) 
for i in range(N-1): 
    ThetaShifted[i] = (Theta[i] + Theta[i+1])/2.0 
ThetaShifted[-1] = (Theta[-1] + 2.0*np.pi)/2.0 
 
# Make the figure 
fig = mpl.figure() 
ax = fig.add_subplot(111, projection='polar') 
bars = ax.bar(Theta, Radii, width=width, bottom=bottom) 
 
# Set the outer ring to be invisible. 
ax.spines["polar"].set_visible(False) 
 
# Set the grid line locations but set the labels to be invisible. 
ax.grid(False) 
ax.set_thetagrids([], visible=False) 
ax.set_rgrids([3], visible=False) 
 
# Apply colors to bars based on the settings above. 
for v, bar in zip(Values, bars): 
    bar.set_facecolor(barcolor) 
    bar.set_edgecolor(bar.get_facecolor()) 
 
# Show the metric and value labels 
for counter in range(N): 
    ax.text(ThetaShifted[counter], bottom-.075*(10+len(MetricLabels[counter])), MetricLabels[counter]+' '*5,  
            horizontalalignment='center', verticalalignment='center',  
            rotation=(counter+.5)*360/N, color=bgcolor, 
            family='monospace') 
    ax.text(ThetaShifted[counter], bottom+1, np.round(Values[counter],2),  
            horizontalalignment='center', verticalalignment='center',  
            rotation=(counter+.5)*360/N, color=bars[counter].get_facecolor(), 
            family='monospace') 
 
# Set the background color to be a dark grey, 
ax.set_axis_bgcolor(bgcolor) 
fig.set_facecolor(bgcolor) 
 
# Show the figure. 
mpl.show()