【问题标题】:Radar Plot Matplotlib Python : how to set label alignmentRadar Plot Matplotlib Python:如何设置标签对齐
【发布时间】:2018-09-04 09:28:20
【问题描述】:

我正在尝试将我的雷达图的标签与轴对齐,但我遇到了困难。

没有标签对齐,我的标签居中并超出了轴线

当我将标签的前半部分(图的右侧)设置为左对齐,后半部分设置为右对齐时,我得到一个非“圆形”对齐

为此,我在 xticklabels 中循环并设置水平对齐方式。

# Draw one axe per variable + add labels labels yet
plt.xticks(angles, categories)
for label,i in zip(ax.get_xticklabels(),range(0,len(angles))):
    if i<len(angles)/2:
        angle_text=angles[i]*(-180/pi)+90
        #label.set_horizontalalignment('left')

    else:
        angle_text=angles[i]*(-180/pi)-90
        #label.set_horizontalalignment('right')
    label.set_rotation(angle_text)

我想有一种正确的方法可以做到这一点,但我无法弄清楚,因为我不知道它是否应该考虑偏移、平移或如何调整极坐标来做到这一点。

感谢您的帮助

保罗

下面是完整代码以获取更多信息

from math import pi
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd

## Generate Data
labels_test=[]
for i in range(0,40):
    labels_test.append("Fooooooooo"+str(i))
pd_radar=pd.DataFrame(data=np.random.randint(low=0, high=10, size=(2, 40)),columns=labels_test)

# number of variable
categories=list(pd_radar)
N = len(categories)

# What will be the angle of each axis in the plot? (we divide the plot / number of variable)
angles = [n / float(N) * 2 * pi for n in range(N)]

# Initialise the spider plot
fig=plt.figure(figsize=(20,10))
ax = plt.subplot(111, polar=True)

# If you want the first axis to be on top:
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)

# Draw one axe per variable + add labels labels yet
plt.xticks(angles, categories)
for label,i in zip(ax.get_xticklabels(),range(0,len(angles))):
    if i<len(angles)/2:
        angle_text=angles[i]*(-180/pi)+90
        label.set_horizontalalignment('left')

    else:
        angle_text=angles[i]*(-180/pi)-90
        label.set_horizontalalignment('right')
    label.set_rotation(angle_text)
# Draw ylabels
ax.set_rlabel_position(0)


# ------- PART 2: Add plots

# Plot each line of the data 

# Ind1
values0=pd_radar.iloc[0].values.flatten().tolist()
ax.plot(angles, values0, linewidth=1, linestyle='solid', label="Label 1",color='yellow')
ax.fill(angles, values0, 'r', alpha=0.1,color='yellow')

# Ind2
values1=pd_radar.iloc[1].values.flatten().tolist()
ax.plot(angles, values1, linewidth=1, linestyle='solid', label="Label 2",color='deepskyblue')
ax.fill(angles, values1, 'r',color='deepskyblue', alpha=0.1)

# Add legend
plt.show()

【问题讨论】:

  • 好的,谢谢,希望更清楚
  • 是的,但我目前无法重现此内容。您使用的是哪个版本的 matplotlib?
  • matplotlib.__version__='2.0.2'

标签: python matplotlib alignment radar-chart


【解决方案1】:

当前代码为does not run with matplotlib 2.2。但是,以下解决方案适用于 OP 正在使用的版本 2.0.2。有关使用较新版本的解决方法,请参阅this question

matplotlib 中的文本有一个rotation mode

旋转模式可以是"default",此时文字先旋转再对齐

也可以是"anchor",在这种情况下,文本首先对齐然后旋转。

因此,为了让刻度标签在极坐标图上指向外部,您需要将文本的对齐方式设置为"left",然后将旋转模式设置为"anchor"

for label,rot in zip(ax.get_xticklabels(),ticks):
    label.set_rotation(rot*180./np.pi)
    label.set_horizontalalignment("left")
    label.set_rotation_mode("anchor")

完整示例:

import numpy as np
import matplotlib.pyplot as plt

fig=plt.figure(figsize=(5,5))
ax = plt.subplot(111, polar=True)

ticks = np.linspace(0, 2*np.pi, 20, endpoint=False)
text = lambda : "".join(np.random.choice(list("manuladefil"), size=10))
labels = [text() for _ in range(len(ticks))]

plt.xticks(ticks, labels, size=16)
for label,rot in zip(ax.get_xticklabels(),ticks):
    label.set_rotation(rot*180./np.pi)
    label.set_horizontalalignment("left")
    label.set_rotation_mode("anchor")

plt.tight_layout()
plt.show()

【讨论】:

    【解决方案2】:

    我找到了一个看起来不太优雅的解决方案。主要思想是将垂直和水平对齐设置为角度的函数。

    from math import pi
    import matplotlib.pyplot as plt
    %matplotlib inline
    import pandas as pd
    import numpy as np
    
    ## Generate Data
    labels_test=[]
    for i in range(0,40):
        labels_test.append("Fooooooooo"+str(i))
    pd_radar=pd.DataFrame(data=np.random.randint(low=0, high=10, size=(2, 40)),columns=labels_test)
    
    
    # number of variable
    categories=list(pd_radar)
    N = len(categories)
    
    # What will be the angle of each axis in the plot? (we divide the plot / number of variable)
    angles = [n / float(N) * 2 * pi for n in range(N)]
    
    # Initialise the spider plot
    fig=plt.figure(figsize=(20,10))
    ax = plt.subplot(111, polar=True)
    
    # If you want the first axis to be on top:
    ax.set_theta_offset(pi / 2)
    ax.set_theta_direction(-1)
    
    # Draw one axe per variable + add labels labels yet
    # idea is to add both vertical and horizontal alignment as a function of pi
    
    plt.xticks(angles, categories)
    for label,i in zip(ax.get_xticklabels(),range(0,len(angles))):
    
        angle_rad=angles[i]
        if angle_rad <= pi/2:
            ha= 'left'
            va= "bottom"
            angle_text=angle_rad*(-180/pi)+90
        elif pi/2 < angle_rad <= pi:
            ha= 'left'
            va= "top"
            angle_text=angle_rad*(-180/pi)+90
        elif pi < angle_rad <= (3*pi/2):
            ha= 'right'
            va= "top"  
            angle_text=angle_rad*(-180/pi)-90
        else:
            ha= 'right'
            va= "bottom"
            angle_text=angle_rad*(-180/pi)-90
        label.set_rotation(angle_text)
        label.set_verticalalignment(va)
        label.set_horizontalalignment(ha)
    
    
    # Draw ylabels
    ax.set_rlabel_position(0)
    
    
    # ------- PART 2: Add plots
    
    # Plot each line of the data 
    
    # Ind1
    values2=pd_radar.iloc[0].values.flatten().tolist()
    ax.plot(angles, values2, linewidth=1, linestyle='solid', label="Label 1",color='yellow')
    ax.fill(angles, values2, 'r', alpha=0.1,color='yellow')
    
    # Ind2
    values3=pd_radar.iloc[1].values.flatten().tolist()
    ax.plot(angles, values3, linewidth=1, linestyle='solid', label="Label 2",color='deepskyblue')
    ax.fill(angles, values3, 'r',color='deepskyblue', alpha=0.1)
    
    # Add legend
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-02
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      相关资源
      最近更新 更多