【问题标题】:Radar plot matplotlib - position of yticks雷达图 matplotlib - yticks 的位置
【发布时间】:2018-12-30 05:16:15
【问题描述】:

我已经完成了雷达图,看起来几乎是我想要的样子。但是,由于情节上的许多值,我想修改ylabels/yticks的对齐方式。

我在以给定角度在一行中创建具有正确值的ylabels / yticks 没有问题,但我希望ylabels 具有相应的值,而不是将它们放在同一行上。所以对于每个角度,应该有两个ylabels 放置在绘图上的相应值附近。在图片中,您可以看到值 4.144.71 正确放置在相应的值处。 我的想法在 Python 中是否可行?

这是我使用的代码:

# number of variable
categories=list(data)[0:]
N = len(categories)

# Angles for plotting
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]

# Initialise
f, ax = plt.subplots(1,1,figsize=(40,20))
ax = plt.subplot(111, polar=True)

# Draw one axe per variable
plt.xticks(angles[:-1], color='grey', size=16)

# the main problem in my code - placing yticks 'around' the plot
# to be near the corresponding values    
for idx, an in enumerate(angles):
    ax.set_rlabel_position(an) # positioning labels at a given angle
    tick_values = [blue_values[idx],red_values[idx]] # to get the two labels values
    plt.yticks(tick_values, [x.rstrip('.0') for x in list(map(str, tick_values))], color="black", size=16)

plt.ylim(0,max(red_values))

# Add plots
# Plot data
ax.plot(angles, blue_values, linewidth=1, linestyle='solid')
# Fill area
ax.fill(angles, blue_values, 'b', alpha=0.5)

# Plot data
ax.plot(angles, red_values, linewidth=1, linestyle='solid')
# Fill area
ax.fill(angles, red_values, 'r', alpha=0.2)

这就是我实现的情节。如您所见,只有两个标签(当然是由于for-loop)。

【问题讨论】:

    标签: python python-3.x matplotlib visualization radar-chart


    【解决方案1】:

    在指定坐标处绘制文本会更容易。

    # number of variable
    categories=list(data)[0:]
    N = len(categories)
    
    # Angles for plotting
    angles = [n / float(N) * 2 * pi for n in range(N)]
    angles += angles[:1]
    
    # Initialise
    f, ax = plt.subplots(1,1,figsize=(40,20))
    ax = plt.subplot(111, polar=True)
    
    # Draw one axe per variable
    plt.xticks(angles[:-1], color='grey', size=16)
    
    # the main problem in my code - placing yticks 'around' the plot
    # to be near the corresponding values
    
    for idx, an in enumerate(angles):
        plt.text(an, red_values[idx], str(red_values[idx]), color="black", size=16)
        plt.text(an, blue_values[idx], str(blue_values[idx]), color="black", size=16)
    
    plt.yticks([])
    # plt.yticks(blue_values + red_values, []) # if you want to keep the circles.
    
    plt.ylim(0,max(red_values))
    
    # Add plots
    # Plot data
    ax.plot(angles, blue_values, linewidth=1, linestyle='solid')
    # Fill area
    ax.fill(angles, blue_values, 'b', alpha=0.5)
    
    # Plot data
    ax.plot(angles, red_values, linewidth=1, linestyle='solid')
    # Fill area
    ax.fill(angles, red_values, 'r', alpha=0.2)
    

    【讨论】:

      猜你喜欢
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多