2

In a matplotlib polar plot, I would like to rotate each individual theta ticklabel by a different angle. However, I cannot find anything in the documentation to do that. Here's a simple plot to illustrate:

from matplotlib import pyplot as plt
import numpy as np

fig = plt.figure()
ax = plt.axes(polar=True)
ax.set_thetalim(0., np.pi/4.)
ax.set_rlim(0., 2.)

# set the size of theta ticklabels (works)
thetatick_locs = np.linspace(0.,45.,4)
thetatick_labels = [u'%i\u00b0'%np.round(x) for x in thetatick_locs]
ax.set_thetagrids(thetatick_locs, thetatick_labels, fontsize=16)

This adds labels at 0, 15, 30 and 45 degrees. What I'd like to do is rotate the 15 degree label by 15 degrees, the 30 degree label by 30 degrees, and so on, so that each label's text direction is radially outward. Since get_xticklabels on a PolarAxes instance seems to get the theta ticklabels, I tried:

for i,t in enumerate(ax.get_xticklabels()):
    t.set_rotation(thetatick_locs[i])

However, that did nothing. Is there any other way of doing what I want? In general, I'm finding that the documentation for polar axes is not as thorough as for rectangular axes, probably because fewer people use it. So maybe there's already a way to do this.

TM5
  • 192
  • 3
  • 12

1 Answers1

5

Your current method works for cartesian coordinates but for polar coordinates, you can use the workaround solution presented earlier here. I have adapted that answer for you below. You can add the following code after setting the theta grids

fig.canvas.draw()

labels = []

for label, angle in zip(ax.get_xticklabels(), thetatick_locs):
    x,y = label.get_position()
    lab = ax.text(x,y, label.get_text(), transform=label.get_transform(),
                  ha=label.get_ha(), va=label.get_va())
    lab.set_rotation(angle)
    labels.append(lab)

ax.set_xticklabels([])

plt.show()    

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71