Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set radial axis on Matplotlib polar plots

I'm plotting an azimuth-elevation curve on a polar plot where the elevation is the radial component. By default, Matplotlib plots the radial value from 0 in the center to 90 on the perimeter. I want to reverse that so 90 degrees is at the center. I tried setting the limits with a call to ax.set_ylim(90,0) but this results in a LinAlgError exception being thrown. ax is the axes object obtained from a call to add_axes.

Can this be done and, if so, what must I do?

Edit: Here is what I'm using now. The basic plotting code was taken from one of the Matplotlib examples

# radar green, solid grid lines
rc('grid', color='#316931', linewidth=1, linestyle='-')
rc('xtick', labelsize=10)
rc('ytick', labelsize=10)

# force square figure and square axes looks better for polar, IMO
width, height = matplotlib.rcParams['figure.figsize']
size = min(width, height)
# make a square figure
fig = figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], projection='polar', axisbg='#d5de9c')

# Adjust radius so it goes 90 at the center to 0 at the perimeter (doesn't work)
#ax.set_ylim(90, 0)

# Rotate plot so 0 degrees is due north, 180 is due south

ax.set_theta_zero_location("N")

obs.date = datetime.datetime.utcnow()
az,el = azel_calc(obs, ephem.Sun())
ax.plot(az, el, color='#ee8d18', lw=3)
obs.date = datetime.datetime.utcnow()
az,el = azel_calc(obs, ephem.Moon())
ax.plot(az, el, color='#bf7033', lw=3)

ax.set_rmax(90.)
grid(True)

ax.set_title("Solar Az-El Plot", fontsize=10)
show()

The plot that results from this is

enter image description here

like image 580
sizzzzlerz Avatar asked Aug 22 '12 14:08

sizzzzlerz


People also ask

How do I change the direction of my axis in Matplotlib?

In Matplotlib we can reverse axes of a graph using multiple methods. Most common method is by using invert_xaxis() and invert_yaxis() for the axes objects. Other than that we can also use xlim() and ylim(), and axis() methods for the pyplot object.

How do I create a third axis in Matplotlib?

Pandas plot() — add the 3rd Y-axis Under the hood, it calls Matplotlib's API by default. We can set the argument secondary_y to True to allow the 2nd chart to be plotted on the secondary Y-axis. To add a third chart with the 3rd Y-axis, we can apply the same trick we did before. ax.


1 Answers

I managed to put he radial axis inverted. I had to remap the radius, in order to match the new axis:

fig = figure()
ax = fig.add_subplot(1, 1, 1, polar=True)

def mapr(r):
   """Remap the radial axis."""
   return 90 - r

r = np.arange(0, 90, 0.01)
theta = 2 * np.pi * r / 90

ax.plot(theta, mapr(r))
ax.set_yticks(range(0, 90, 10))                   # Define the yticks
ax.set_yticklabels(map(str, range(90, 0, -10)))   # Change the labels

Note that is just a hack, the axis is still with the 0 in the center and 90 in the perimeter. You will have to use the mapping function for all the variables that you are plotting.

like image 66
Pablo Navarro Avatar answered Sep 28 '22 00:09

Pablo Navarro