Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse radial axes of Matplotlib polar plot

I'm trying to create an astronomical polar plot with a radial axis that starts from -45° on outer line and increases to 90° in the center of the plot. But I didn't find any way to reverse radial axis of the PolarAxes instance. invert_yaxis() method doesn't work at all. Also, there are some hidden methods such as ax.set_rlim() that doesn't have any docs.

Here is my current code:

fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8], polar=True)
# ax.invert_yaxis()
ax.set_theta_zero_location('N')
ax.set_ylim(-45, 90)
ax.set_yticks(np.arange(-45, 90, 15))
ax.plot(ras, decs, linestyle='', marker='.')

and my plot

like image 561
Hello Human Avatar asked Mar 29 '17 01:03

Hello Human


People also ask

How do I reverse axes 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 you change the axis of a plot in Matplotlib?

To change the range of X and Y axes, we can use xlim() and ylim() methods.

How do I get rid of Y axis in Matplotlib?

If we just want to turn either the X-axis or Y-axis off, we can use plt. xticks( ) or plt. yticks( ) method respectively.

What is axes in Matplot?

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.


2 Answers

Old question, but I finally found an easy answer. Use ax.set_rlim(bottom=90, top=-45). Note that you have to set this BEFORE calling ax.plot(), or it will not transform the plotted points accordingly.

like image 89
smp55 Avatar answered Nov 03 '22 21:11

smp55


It's a little bit "hacky", but if you know the bounds (which it seems like you do as it corresponds to declination) you could do something like:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8], polar=True)
# ax.invert_yaxis()
ax.set_theta_zero_location('N')
ax.set_rlim(90, -45, 1)
# Note: you must set the end of arange to be slightly larger than 90 or it won't include 90
ax.set_yticks(np.arange(-45, 91, 15))
ax.set_yticklabels(ax.get_yticks()[::-1])
ax.plot([0,10,20], 90-np.array([12,13,14]), linestyle='', marker='.')
fig.show()

enter image description here

like image 31
Robbie Avatar answered Nov 03 '22 19:11

Robbie