Matplotlib Polar Plot - Overriding major tic axis labels

Viewed 26

Using a list with 360 entries, I am creating a polar chart with MatplotLib. This works nicely:

%matplotlib widget

fig = plt.figure()
ax = fig.add_subplot(projection='polar')
for rad in np.arange(0, (2 * np.pi), 2 * np.pi / 360):
    plt.polar(rad, random.random()*10, marker='.', markersize=2, color='green')
plt.show()

But I get degrees 0 through 360, with 0 at "3pm". What I want is to have labels at 3, 6, 9 and 12 be, respectively, Front, Right, Back and Left. And then I want to rotate the whole thing so that Front as at 12.

1 Answers

You could use ax.set_theta_zero_location with whatever direction you want (East,West,South,North) to change the location of the origin 0. See doc here and example here.

See example below with the code you provided:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection='polar')
for rad in np.arange(0, (2 * np.pi), 2 * np.pi / 360):
    plt.polar(rad, np.random.random()*10, marker='.', markersize=2, color='green')
ax.set_theta_zero_location("N") #Set location to "North" to have 0 on top
plt.show()

And the output returns:

enter image description here

EDIT:

To change the ticks to "Front",etc, you can use: ax.set_xticklabels(['Front','','Left','','Back','','Right'])

See output below:

enter image description here

Related