gpt4 book ai didi

python - 极坐标/径向图上的刻度标签填充和刻度标签位置 - matplotlib

转载 作者:行者123 更新时间:2023-12-04 13:54:49 33 4
gpt4 key购买 nike

我正在尝试旋转附加图上的径向刻度标签。
当我指定了“旋转”命令时,为什么 matplotlib 不旋转它们?
然后我想在径向方向上移动标签。是否有与极坐标图等效的“pad”命令?

import numpy as np
import matplotlib.pyplot as plt
import math

Graph_title = "Radar Plot"

def radarplot():
ax = plt.subplot(111, polar=True)

# INPUT DATA
n_directions = 12
angles = [n / float(n_directions) * 2 * math.pi for n in range(n_directions)]
data = [3.0, 3.0, 3.0, 3.0, 2.0, 2.5, 2.5, 2.5, 2.75, 2.75, 3.0, 3.0]

# Add the last element of the list to the list. This is necessary or the line from 330 deg to 0 degree does not join up on the plot.
angles = np.append(angles, angles[:1])
data = np.append(data, data[:1])

ax.plot(angles, data, linewidth=2, linestyle='solid', color = 'red')

# Radial tick parameters
radial_ticks = [0.00, 0.50, 1.00, 1.50, 2.00, 2.50, 3.00]
ax.set_rlabel_position(45)
ax.set_rorigin(0)
plt.yticks(radial_ticks, color='black', size=8)
ax.set_yticklabels(radial_ticks, rotation = 45, zorder = 500)

# X Tick parameters
plt.xticks(angles, color='black', size=10, zorder = 5)
ax.tick_params(axis='x', which='major', pad=3)
ax.set_theta_zero_location("N") # Sets the labels initial position to 0 degrees
ax.set_theta_direction("clockwise") # Set the labels to rotate clockwise


plt.savefig(Graph_title +".png", figsize = [6.4, 5], dpi=1000)
plt.show()
plt.close()

radarplot()
enter image description here

最佳答案

最近我想达到和你一样的目的,这是我想出的解决方案。

  • 使用命令 ax.set_yticklabels([]) 取消自动 r 刻度标签
  • 为每个径向刻度定义一个刻度列表、一个位置列表和一个对齐列表。
  • 使用 text命令在位置列表指定的径向位置写入刻度列表中的值,对齐方式由对齐列表指定。

  • 通过更改位置列表中的值,基本上可以沿径向移动 r 个刻度。
    确保 text命令由 transform=ax.transData 指定选项。
    import numpy as np
    import matplotlib.pyplot as plt
    import math

    Graph_title = "Radar Plot"

    def radarplot():
    ax = plt.subplot(111, polar=True)

    # INPUT DATA
    n_directions = 12
    angles = [n / float(n_directions) * 2 * math.pi for n in range(n_directions)]
    data = [3.0, 3.0, 3.0, 3.0, 2.0, 2.5, 2.5, 2.5, 2.75, 2.75, 3.0, 3.0]

    # Add the last element of the list to the list. This is necessary or the line from 330 deg to 0 degree does not join up on the plot.
    angles = np.append(angles, angles[:1])
    data = np.append(data, data[:1])

    ax.plot(angles, data, linewidth=2, linestyle='solid', color = 'red')

    r_ticks = [0.00, 0.50, 1.00, 1.50, 2.00, 2.50, 3.00] #tick list
    r_ticks_pos = [0.20, 0.65, 1.15, 1.65, 2.15, 2.65, 3.25] #radial position list (CHANGE THESE VALUES TO MOVE EACH TICK RADIALLY INDIVIDUALLY)
    r_ticks_h_align = ['center','center','center','center','center','center','center'] #horizontal alignment list
    r_ticks_v_align = ['center','center','center','center','center','center','center'] #vertical alignment list
    r_label_angle = 45 #theta angle

    # Radial tick parameters
    ax.set_rlabel_position(r_label_angle)
    ax.set_rorigin(0)

    ax.set_yticklabels([])


    ax.set_rticks(r_ticks)
    ax.set_rlabel_position(r_label_angle)

    #write the ticks using the text command
    for rtick, rtick_pos, rtick_ha, rtick_va in zip(r_ticks, r_ticks_pos, r_ticks_h_align, r_ticks_v_align):
    plt.text(np.radians(r_label_angle), rtick_pos, r'$'+str(rtick)+'$', ha=rtick_ha, va=rtick_va, transform=ax.transData, rotation=-45, fontsize=8)

    # X Tick parameters
    plt.xticks(angles, color='black', size=10, zorder = 5)
    ax.tick_params(axis='x', which='major', pad=3)
    ax.set_theta_zero_location("N") # Sets the labels initial position to 0 degrees
    ax.set_theta_direction("clockwise") # Set the labels to rotate clockwise


    plt.savefig(Graph_title +".png", figsize = [6.4, 5], dpi=1000)
    plt.show()
    plt.close()

    radarplot()
    结果:
    enter image description here

    关于python - 极坐标/径向图上的刻度标签填充和刻度标签位置 - matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64313104/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com