如何在Matplotlib中的极坐标图中移动ytick标签

时间:2018-02-18 21:10:06

标签: python matplotlib

How my polar chart looks like

我试图移动ytick标签,使它们与轴标签对齐。例如,' PTS' label与其轴水平对齐,但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)

1 个答案:

答案 0 :(得分:0)

我想你要设置

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

enter image description here