python matplolib polar chart x-axis label position

2019-05-22 12:46发布

问题:

I'm using (maybe misusing?) a Polar chart to represent some data I have. I would like to have the labels, currently positioned right above the x-axis, to be above the segment of data. So right between the axis.

I've found multiple articles on how to rotate everything but it always keeps the label right above the axis.

The peace of code I have for the axis (if you need the whole definition/code, please say so):

# Set axis names and orientation
ax.set_theta_zero_location("N")
ax.set_xticklabels(['Seg 1', 'Seg 2', 'Seg 3', 'Seg 4', 'Seg 5', 'Seg 6', 'Seg 7', 'Seg 8'])
ax.set_ylim((0, 10.0))
ax.set_rgrids([5,10], angle=22)

The current image it produces:

Now I'd like the labels 'Seg 1', 'Seg 2', etc. to be moved from the axis to right between the axis.

Is there any way to do that?

回答1:

you can do this by setting the labels on minor ticks, but then setting the minor tick width to zero so you don't see them:

import matplotlib.ticker as ticker

# Set the major and minor tick locations
ax.xaxis.set_major_locator(ticker.MultipleLocator(np.pi/4))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(np.pi/8))

# Turn off major tick labels
ax.xaxis.set_major_formatter(ticker.NullFormatter())

# Set the minor tick width to 0 so you don't see them
for tick in ax.xaxis.get_minor_ticks():
    tick.tick1line.set_markersize(0)
    tick.tick2line.set_markersize(0)
    tick.label1.set_horizontalalignment('center')

# Set the names of your ticks, with blank spaces for the major ticks
ax.set_xticklabels(['','','Seg 1','','Seg 2','','Seg 3','','Seg 4','','Seg 5','','Seg 6','','Seg 7','','Seg 8'],minor=True)