Adding a radial axis label to matplotlib

I am making a polar scatter plot for a college project with matplotlib, and I cannot learn how to add a label to the radial axis. Here is my code (I forgot the data because it was read from 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 cannot find any direct method for this. Has anyone dealt with this before and have any suggestions?

+5
source share
3 answers
 from pylab import * N = 150 r = 2*rand(N) theta = 2*pi*rand(N) area = 200*r**2*rand(N) colors = theta ax = subplot(111, polar=True) c = scatter(theta, r, c=colors, s=area, cmap=cm.hsv) c.set_alpha(0.75) ax.set_ylabel('Radius', rotation=45, size=11) show() 

enter image description here

+1
source

I don't know how to do this, but you can use ax.text to make your own. You can get the position of the radial tick marks with ax.get_rlabel_position() , and the midpoint of the radial axis with ax.get_rmax()/2.

For example, here is 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 is the conclusion:

enter image description here

I would be interested to know if there is a more elegant solution, but I hope this helps you.

0
source

A slightly different method from @tom. This directly uses the plt.legend parameter.

Example:

 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,label='Supernova') ax.set_title("Spread of Abell Cluster Supernova Events as a Function of Fractional Radius", va='bottom') ax.legend(loc='lower right', scatterpoints=1) plt.show() 

You can change lower right to upper right or even best to leave legend alignment in matplotlib.

enter image description here

0
source

All Articles