【问题标题】:How to move ytick labels in a polar plot in Matplotlib如何在 Matplotlib 的极坐标图中移动 ytick 标签
【发布时间】:2018-02-18 21:10:06
【问题描述】:

How my polar chart looks like

我正在尝试移动 ytick 标签,以便它们与轴标签类似地对齐。例如,“PTS”标签与其轴水平对齐,但 ytick 标签向右移动。

对于 y 值标签,我使用 set_rgrids() 函数指定角度和标签值,并使用 tick_params() 函数指定标签属性,如旋转。但是此功能不允许我设置标签的文本框对齐方式。

如何将 y 值向左移动,以便它们直接位于相应的轴标签下方?

for i in range(N):
    #rgrids() for y-labels, tick_params() for properties of y-labels
    axes[i].set_rgrids(rgrids, angle=angles[i], labels=rlabels[i])
    axes[i].tick_params('y', labelrotation=(angles[i]-180 if 90<angles[i]<270 else angles[i]))

    axes[i].set_ylim(ymin, ymax)
    axes[i].set_theta_offset(np.deg2rad(90))
    #text() for axis label
    axes[i].text(np.deg2rad(angles[i]), 1.08*ymax,cat[i], va="center", ha='center',  size=13, rotation=(angles[i]-180 if 90<angles[i]<270 else angles[i]))

完整代码:

import numpy as np
import matplotlib.pyplot as pl

def normalize(val, min=0, max=100):
    value = val-min
    range = max-min
    frac = float(value)/range
    return frac*100

cat = ['PTS', 'FG%', 'FGM', '3PM', '3FG%']
N = len(cat)
angles = np.arange(0, 360, 360.0/N)

rcount = 9
ranges = [[5, 33], [30, 55], [3, 10.2], [0.1, 4.5], [22, 46]]
rlabels = [[0]*rcount] * N
ymin = 0
ymax = 100

for i in range(N):
    size = ranges[i][1] - ranges[i][0]
    step = float(size)/rcount
    rlabels[i] = list(np.arange(ranges[i][0] + step, ranges[i][1] + step, step))
    rlabels[i] = [round(x,1) for x in rlabels[i]]

step = (ymax - ymin)/float(rcount)
rgrids = list(np.arange(ymin + step, ymax + step, step))

rect = [0.05, 0.05, 0.8, 0.8]
fig = pl.figure(figsize=(7, 7))

axes = [fig.add_axes(rect, projection="polar", label="axes%d" % i) for i in range(N)]
axes[0].set_thetagrids(angles, labels=cat, fontsize=14, visible=False)#jesli visible=False to trzeba dodac recznie za pomocą ax.text()
axes[0].grid(color='gray', linewidth = 1)

for labels in rlabels:
    labels[0] = ''

for i in range(N):
    axes[i].set_rgrids(rgrids, angle=angles[i], labels=rlabels[i])
    axes[i].tick_params('y',  labelrotation=(angles[i]-180 if 90<angles[i]<270 else angles[i]))
    axes[i].set_ylim(ymin, ymax)
    axes[i].set_theta_offset(np.deg2rad(90))
    axes[i].text(np.deg2rad(angles[i]), 1.08*ymax,cat[i], va="center", ha='center',  size=13, rotation=(angles[i]-180 if 90<angles[i]<270 else angles[i]))

for i in range(1, N):
    axes[i].spines["polar"].set_visible(False)
    axes[i].patch.set_visible(False)
    axes[i].grid("off")
    axes[i].xaxis.set_visible(False)

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    我想你想设置

    ax.text(..., ha='left', rotation_mode="anchor", ...)
    

    【讨论】:

    • 似乎是一个很好的解决方案,尽管我想知道是否有一种简单的方法可以让轴标签保持原样并移动值标签,而不是相反。无论如何感谢您的帮助。
    • 我想解决方案可能类似于this question 中的解决方案,只是针对 yticklabels。不过有点hacky。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2013-06-14
    • 2014-03-13
    • 2019-08-25
    • 1970-01-01
    相关资源
    最近更新 更多