Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Radial Axis Label in matplotlib

I am making a polar scatter plot for a college project with matplotlib and I can't find out how to add a label to the radial axis. Here is my code ( I left out the data because it was read out of a csv)

import matplotlib.pyplot as plt

ax = plt.subplot(111, polar=True)
ax.set_rmax(1)
c = plt.scatter(theta, radii)
ax.set_title("Spread of Abell Cluster Supernova Events as a Function of Fractional Radius", va='bottom')
ax.legend(['Supernova'])
plt.show()

(My plot looks like this. I can't seem to find any straight forward method to do it. Has anyone dealt with this before and have any suggestions?

like image 225
Matt T Avatar asked Aug 14 '15 02:08

Matt T


People also ask

How do I add axis labels in Matplotlib?

Use the xlabel() method in matplotlib to add a label to the plot's x-axis.

Can you label points in Matplotlib?

To label the scatter plot points in Matplotlib, we can use the matplotlib. pyplot. annotate() function, which adds a string at the specified position.


1 Answers

I don't know of a built in way to do it, but you could use ax.text to make your own. You can get the position of the radial tick labels using ax.get_rlabel_position(), and the mid point of the radial axis using ax.get_rmax()/2.

For example, here's your code (with some random data):

import matplotlib.pyplot as plt
import numpy as np

theta=np.random.rand(40)*np.pi*2.
radii=np.random.rand(40)

ax = plt.subplot(111, polar=True)
ax.set_rmax(1)
c = plt.scatter(theta, radii)
ax.set_title("Spread of Abell Cluster Supernova Events as a Function of Fractional Radius", va='bottom')
ax.legend(['Supernova'])

label_position=ax.get_rlabel_position()
ax.text(np.radians(label_position+10),ax.get_rmax()/2.,'My label',
        rotation=label_position,ha='center',va='center')

plt.show()

And here's the output:

enter image description here

I'd be interested to see if there's a more elegant solution, but hopefully this helps you.

like image 53
tmdavison Avatar answered Sep 25 '22 01:09

tmdavison