Как установить Half Polar Plot на север с помощью matplotlib - PullRequest
2 голосов
/ 03 октября 2019

У меня есть функция (основанная на этом: https://gist.github.com/kanhua/fce2421c2c4eb356a9d1d9e1cb3f61fc), которая создает полуполярный график с использованием matplotlib.transforms.Affine2D, matplotlib.projections.PolarAxes и mpl_toolkits.axisartist.

Это кодмоя функция:

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

Вызвав такую ​​функцию:

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')

Я создал следующий график:

enter image description here

Проблема в том, что я хочу сделать полярный график очень похожим на этот, но я хочу, чтобы ось тета переместилась с 90 градусов на -90 градусов (справа налево), что является противоположным направлением моегофункция в настоящее время строит графики.

Кто-нибудь знает, как я могу инвертировать направление, в котором строится тэта?

Я знаю, что matplotlib.projections.PolarAxes имеет аргумент, называемый theta_direction который установлен как 1 по умолчанию, но я не мог понять, где в коде это должно идти.

1 Ответ

1 голос
/ 03 октября 2019

Вы можете добиться того же, используя:

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

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...