Home > Back-end >  How to set limits around (on both sides of) 0, in a polar Matplotlib plot (wedge diagram)
How to set limits around (on both sides of) 0, in a polar Matplotlib plot (wedge diagram)

Time:03-11

I am making a wedge diagram (plotting quasars in space, with RA as theta and Dec as r). I need to set the limits of a polar plot on both sides of 0. My limits should go from 45 degrees to 315 degrees with 0 degrees in between those two values (45-0-315). How do I do this?

This is my code:

import numpy as np
import matplotlib.pyplot as plt

theta = (np.pi/180)*np.array([340.555906,3.592373,32.473440,33.171584,35.463857,44.268397,339.362504,345.211906,346.485567,346.811945,348.672405,349.180736,349.370850,353.098343])
r = np.array([-32.906663,-33.842402,-32.425917,-32.677975, -30.701083,-31.460307,-32.909861,-30.802969,-33.683759,-32.207783,-33.068686,-33.820102,-31.438195,-31.920375])

colors = 'red'

fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
c = ax.scatter(theta, r, c=colors, cmap='hsv', alpha=0.75)

plt.show()

If I put the limits:

ax.set_thetamin(45)
ax.set_thetamax(-45)

I get the correct slice of the diagram, but the wrong values on the theta axis (the axis now goes from -45-45 degrees).

If I put the limits:

ax.set_thetamin(45)
ax.set_thetamax(315)

I get the wrong slice of the diagram, but the correct values on the theta axis.

What to do?

CodePudding user response:

It appears that matplotlib will only make the theta limits span across theta=0 if you have a positive and negative value for thetamin and thetamax. From the docstring for enter image description here

For completeness, here I put it all together in your script:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

theta = (np.pi/180)*np.array([340.555906,3.592373,32.473440,33.171584,35.463857,44.268397,339.362504,345.211906,346.485567,346.811945,348.672405,349.180736,349.370850,353.098343])
r = np.array([-32.906663,-33.842402,-32.425917,-32.677975, -30.701083,-31.460307,-32.909861,-30.802969,-33.683759,-32.207783,-33.068686,-33.820102,-31.438195,-31.920375])

colors = 'red'

fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
c = ax.scatter(theta, r, c=colors, cmap='hsv', alpha=0.75)

ax.set_thetamin(45)
ax.set_thetamax(-45)

fmt = lambda x, pos: "{:g}".format(np.degrees(x if x >= 0 else x   2 * np.pi))
ax.xaxis.set_major_formatter(ticker.FuncFormatter(fmt))

plt.show()
  • Related