Reputation: 696
I have a function (based on this: https://gist.github.com/kanhua/fce2421c2c4eb356a9d1d9e1cb3f61fc) which produces a half-polar plot
by using matplotlib.transforms.Affine2D
, matplotlib.projections.PolarAxes
and mpl_toolkits.axisartist
.
This is the code of my function:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.transforms import Affine2D
from matplotlib.projections import PolarAxes
from mpl_toolkits.axisartist import angle_helper
from mpl_toolkits.axisartist.grid_finder import MaxNLocator, DictFormatter
from mpl_toolkits.axisartist.floating_axes import GridHelperCurveLinear, FloatingSubplot
def fractional_polar_axes(f, thlim=(-90, 90), rlim=(0, .48),step=(30, 0.12),thlabel='J-H vs J slope', rlabel='length', ticklabels=True, theta_offset=0, rlabels = None):
'''
Return polar axes that adhere to desired theta (in deg) and r limits.
steps for theta and r are really just hints for the locators.
'''
th0, th1 = thlim # deg
r0, r1 = rlim
thstep, rstep = step
tr_rotate = Affine2D().translate(theta_offset, 0)
# scale degrees to radians:
tr_scale = Affine2D().scale(np.pi/180., 1.)
pa = PolarAxes
tr = tr_rotate + tr_scale + pa.PolarTransform()
theta_grid_locator = angle_helper.LocatorDMS((th1-th0)//thstep)
r_grid_locator = MaxNLocator((r1-r0)//rstep)
theta_tick_formatter = angle_helper.FormatterDMS()
if rlabels:
rlabels = DictFormatter(rlabels)
grid_helper = GridHelperCurveLinear(tr,
extremes=(th0, th1, r0, r1),
grid_locator1=theta_grid_locator,
grid_locator2=r_grid_locator,
tick_formatter1=theta_tick_formatter,
tick_formatter2=rlabels)
a = FloatingSubplot(f, 111, grid_helper=grid_helper)
f.add_subplot(a)
# adjust x axis (theta):
print(a)
a.axis["bottom"].set_visible(False)
a.axis["top"].set_axis_direction("bottom") # tick direction
a.axis["top"].toggle(ticklabels=ticklabels, label=bool(thlabel))
a.axis["top"].major_ticklabels.set_axis_direction("top")
a.axis["top"].label.set_axis_direction("top")
a.axis["top"].major_ticklabels.set_pad(10)
# adjust y axis (r):
a.axis["left"].set_axis_direction("bottom") # tick direction
a.axis["right"].set_axis_direction("top") # tick direction
a.axis["left"].toggle(ticklabels=True, label=bool(rlabel))
# add labels:
a.axis["top"].label.set_text(thlabel)
a.axis["left"].label.set_text(rlabel)
# create a parasite axes whose transData is theta, r:
auxa = a.get_aux_axes(tr)
print(auxa)
# make aux_ax to have a clip path as in a?:
auxa.patch = a.patch
# this has a side effect that the patch is drawn twice, and possibly over some other
# artists. So, we decrease the zorder a bit to prevent this:
a.patch.zorder = -2
# add sector lines for both dimensions:
thticks = grid_helper.grid_info['lon_info'][0]
rticks = grid_helper.grid_info['lat_info'][0]
print(grid_helper.grid_info['lat_info'])
for th in thticks[1:-1]: # all but the first and last
auxa.plot([th, th], [r0, r1], ':', c='k', zorder=-1, lw=1.)
for ri, r in enumerate(rticks):
# plot first r line as axes border in solid black only if it isn't at r=0
if ri == 0 and r != 0:
ls, lw, color = 'solid', 1, 'k'
else:
ls, lw, color = 'dashed', 0.5, 'k'
# From http://stackoverflow.com/a/19828753/2020363
auxa.add_artist(plt.Circle([0, 0], radius=r, ls=ls, lw=lw, color=color,
fill=False,transform=auxa.transData._b, zorder=1000))
return auxa
By calling the function like that:
f1 = plt.figure(facecolor='white',figsize = (5,5),dpi=100)
a1 = fractional_polar_axes(f1, thlim=(-90,90),rlim=(0, 1.4),step=(30, 0.30),
theta_offset=90,thlabel='J vs. J-H')
I produced the following plot:
The problem is: I want to make a polar plot very similar to this one, but I want the theta axis to go from 90deg to -90deg (from right to left), which is the contrary direction of what my function currently plots.
Does anyone knows how can I invert the direction in which theta is plotted?
I am aware that matplotlib.projections.PolarAxes
has an argument called theta_direction
which is set as 1 as default, but I couldn't figure out where in the code this should go.
Upvotes: 2
Views: 3014
Reputation: 1629
You could achieve the same using:
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar', xlim=(-90, 90))
ax.set_theta_direction(-1) # change direction to CCW
ax.set_thetamin(-90) # set the limits
ax.set_thetamax(90)
ax.set_theta_offset(.5*np.pi) # point the origin towards the top
ax.set_thetagrids(range(-90, 120, 30)) # set the gridlines
ax.set_title('J vs. J-H', pad=-50) # add title and relocate negative value lowers the location
ax.set_xlabel('xlabel')
ax.xaxis.set_label_coords(0.5, 0.15) # change the location of the xlabel to given x, y locations w.r.t. the entire figure
Upvotes: 2