如何使用 matplotlib 将半极坐标图设置为北

朱莉娅火箭

我有一个函数(基于此: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')

我制作了以下情节:

在此处输入图片说明

问题是:我想制作一个与此非常相似的极坐标图,但我希望 theta 轴从 90deg 到 -90deg(从右到左),这与我的函数当前绘制的方向相反。

有谁知道如何反转绘制 theta 的方向?

我知道matplotlib.projections.PolarAxes有一个名为theta_direction1 作为默认设置的参数,但我无法弄清楚它应该在代码中的哪个位置。

马克

您可以使用以下方法实现相同的效果:

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

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在matplotlib中设置极坐标图的大小?

来自分类Dev

matplotlib极坐标图设置标签位置

来自分类Dev

如何更改matplotlib极坐标图的“ r”轴位置?

来自分类Dev

如何在 matplotlib 中计算极坐标图密度

来自分类Dev

在matplotlib极坐标图中设置theta-ticks

来自分类Dev

使用色图设置极坐标图的背景色

来自分类Dev

如何绘制极坐标图?

来自分类Dev

Matplotlib:极坐标图坐标轴刻度标签位置

来自分类Dev

在matplotlib极坐标图上旋转theta = 0

来自分类Dev

在matplotlib中的极坐标图上移动径向刻度标签

来自分类Dev

Matplotlib-在极坐标图中绘制平滑圆

来自分类Dev

极坐标图多边形的Python / Matplotlib逆填充

来自分类Dev

极坐标图在matplotlib中给出了错误的角度

来自分类Dev

Matplotlib极坐标图未在应绘制的位置绘制

来自分类Dev

极坐标图多边形的Python / Matplotlib逆填充

来自分类Dev

如何使用MATPLOTLIB设置标签?

来自分类Dev

极坐标图中元素的matplotlib zorder叠加在笛卡尔坐标图上

来自分类Dev

在Python中使用Facetgrid的极坐标图

来自分类Dev

使用北向和东向坐标的Python matplotlib底图

来自分类Dev

如何在极坐标图中使用颜色条?

来自分类Dev

使用 matplotlib 的极坐标流图范围不好

来自分类Dev

Matplotlib 并使用极坐标投影去除子图之间的空白

来自分类Dev

Matplotlib极坐标线

来自分类Dev

为极坐标图的中心设置负值

来自分类Dev

如何使用Matplotlib用极坐标中的轮廓密度线绘制散点图?

来自分类Dev

如何在Matplotlib中使用坐标?

来自分类Dev

如何使用matplotlib在子图中设置标题?

来自分类Dev

如何使用matplotlib为复杂功能设置动画?

来自分类Dev

如何使用 matplotlib 为圆圈和线条设置动画?

Related 相关文章

  1. 1

    如何在matplotlib中设置极坐标图的大小?

  2. 2

    matplotlib极坐标图设置标签位置

  3. 3

    如何更改matplotlib极坐标图的“ r”轴位置?

  4. 4

    如何在 matplotlib 中计算极坐标图密度

  5. 5

    在matplotlib极坐标图中设置theta-ticks

  6. 6

    使用色图设置极坐标图的背景色

  7. 7

    如何绘制极坐标图?

  8. 8

    Matplotlib:极坐标图坐标轴刻度标签位置

  9. 9

    在matplotlib极坐标图上旋转theta = 0

  10. 10

    在matplotlib中的极坐标图上移动径向刻度标签

  11. 11

    Matplotlib-在极坐标图中绘制平滑圆

  12. 12

    极坐标图多边形的Python / Matplotlib逆填充

  13. 13

    极坐标图在matplotlib中给出了错误的角度

  14. 14

    Matplotlib极坐标图未在应绘制的位置绘制

  15. 15

    极坐标图多边形的Python / Matplotlib逆填充

  16. 16

    如何使用MATPLOTLIB设置标签?

  17. 17

    极坐标图中元素的matplotlib zorder叠加在笛卡尔坐标图上

  18. 18

    在Python中使用Facetgrid的极坐标图

  19. 19

    使用北向和东向坐标的Python matplotlib底图

  20. 20

    如何在极坐标图中使用颜色条?

  21. 21

    使用 matplotlib 的极坐标流图范围不好

  22. 22

    Matplotlib 并使用极坐标投影去除子图之间的空白

  23. 23

    Matplotlib极坐标线

  24. 24

    为极坐标图的中心设置负值

  25. 25

    如何使用Matplotlib用极坐标中的轮廓密度线绘制散点图?

  26. 26

    如何在Matplotlib中使用坐标?

  27. 27

    如何使用matplotlib在子图中设置标题?

  28. 28

    如何使用matplotlib为复杂功能设置动画?

  29. 29

    如何使用 matplotlib 为圆圈和线条设置动画?

热门标签

归档