【问题标题】:Plotting sector of polar plot (so a wedge) in matplotlib在 matplotlib 中绘制极坐标图的扇区(所以是一个楔形)
【发布时间】:2017-10-26 14:35:27
【问题描述】:

所以我试图在极坐标图中生成一个赤经 (RA) 作为红移 (z) 函数的图,但我只需要极坐标图中的一个扇区,所以基本上我只需要一个楔形 (底部图像右侧的最后一个图是我想要的)。我一直在使用 matplotlib 的示例代码 demo_floating_axes.py,但仍然遇到一些问题。我已经能够加载我的数据并将其绘制出来,但是我对示例代码不够熟悉,无法调整我需要调整的东西(特别是楔形的方向和 RA 上的标签轴。这是我一直在使用的。大部分来自 maplotlib 示例代码。

from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
import numpy as np
import mpl_toolkits.axisartist.angle_helper as angle_helper
from matplotlib.projections import PolarAxes
from mpl_toolkits.axisartist.grid_finder import (FixedLocator, MaxNLocator,
                                             DictFormatter)
import matplotlib.pyplot as plt

plt.ion()

def setup_axes3(fig, rect):
"""
Sometimes, things like axis_direction need to be adjusted.
"""

    # rotate a bit for better orientation
    tr_rotate = Affine2D().translate(-95, 0)

    # scale degree to radians
    tr_scale = Affine2D().scale(np.pi/180., 1.)

    tr = PolarAxes.PolarTransform() #tr_rotate + tr_scale + PolarAxes.PolarTransform()

    grid_locator1 = angle_helper.LocatorHMS(4)
    tick_formatter1 = angle_helper.FormatterHMS()

    grid_locator2 = MaxNLocator(3)

    ra0, ra1 = 35.48, 35.54    #max and min RA vals
    cz0, cz1 = 0, 3.5          #max and min z vals
    grid_helper = floating_axes.GridHelperCurveLinear(
        tr, extremes=(ra0, ra1, cz0, cz1),
        grid_locator1=grid_locator1,
        grid_locator2=grid_locator2,
        tick_formatter1=tick_formatter1,
        tick_formatter2=None)

    ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
    fig.add_subplot(ax1)

    # adjust axis
    ax1.axis["left"].set_axis_direction("bottom")
    ax1.axis["right"].set_axis_direction("top")
    ax1.axis["bottom"].set_visible(False)
    ax1.axis["top"].set_axis_direction("bottom")
    ax1.axis["top"].toggle(ticklabels=True, label=True)
    ax1.axis["top"].major_ticklabels.set_axis_direction("top")
    ax1.axis["top"].label.set_axis_direction("top")
    ax1.axis["left"].label.set_text(r"z")
    ax1.axis["top"].label.set_text(r"RA")

    # create a parasite axes whose transData in RA, cz
    aux_ax = ax1.get_aux_axes(tr)

    aux_ax.patch = ax1.patch  # for aux_ax to have a clip path as in ax
    ax1.patch.zorder = 0.9  # but 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.

    return ax1, aux_ax
################################
fig = plt.figure()

dat = np.loadtxt('Master_A.tab')
z = dat[:,5]                        #redshifts
ra = dat[:,66]                      # RA in degrees

ax3, aux_ax3 = setup_axes3(fig, 111)

theta = ra 
radius = z 
aux_ax3.scatter(theta, radius)

plt.show()

我得到了一个我想要的输出(显然我还不能嵌入图像,所以我不能包含我得到的输出的图片,但就像我说的那样,我正在尝试得到一些东西类似于下面示例图像右侧的最后一个图),但由于某种原因,它将楔形旋转 90 度并将我的 RA 度转换为我不想要的六十进制,但我不知道它在哪里转换它在代码中。因此,如果有人可以为此提供任何帮助,我将不胜感激!此外,我最终将需要使用 RA 和 Dec 作为红移的函数进入 3D,所以如果有人对我如何将其变为 3d 有任何指示,那也太棒了!

【问题讨论】:

  • 刻度和刻度标签由定位器和格式化程序确定,在这种情况下 grid_locator1 和 tick_formatter1 用于角轴。选择不同的代码以获得不同的刻度。不清楚你所说的“它旋转了 90 度”是什么意思。您可以使用Affine2D().translate 和参数ra0, ra1 来旋转和设置比例。显然,这个问题真的不清楚,我认为无法回答。
  • 当我说它旋转了 90 度时,我的意思是与示例图片相比。在示例图片中,它的点位于图的底部,圆形部分位于图的顶部。当我使用上面的代码时,我得到相反的方向,它更像是一个非常薄的切片而不是一个楔子,所以你无法真正看到这些点是如何分布的。就代码而言,我似乎找不到有关 angle_helper 函数的任何文档,所以我不知道 tick_formatter1 = angle_helper.FormatterHMS() 行在做什么。

标签: python matplotlib plot astronomy sector


【解决方案1】:

您可以从没有 HMS 角度的更简单的版本开始。

import mpl_toolkits.axisartist.floating_axes as floating_axes
import numpy as np
from matplotlib.projections import PolarAxes
import matplotlib.pyplot as plt

def setup_axes3(fig, rect):
    tr = PolarAxes.PolarTransform() 

    ra0, ra1 = 0, np.pi/2.    #max and min RA vals
    cz0, cz1 = 0, 1          #max and min z vals
    grid_helper = floating_axes.GridHelperCurveLinear(
        tr, extremes=(ra0, ra1, cz0, cz1))

    ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
    fig.add_subplot(ax1)

    # adjust axis
    ax1.axis["left"].set_axis_direction("bottom")
    ax1.axis["right"].set_axis_direction("top")
    ax1.axis["bottom"].set_visible(False)
    ax1.axis["top"].set_axis_direction("bottom")
    ax1.axis["top"].toggle(ticklabels=True, label=True)
    ax1.axis["top"].major_ticklabels.set_axis_direction("top")
    ax1.axis["top"].label.set_axis_direction("top")
    ax1.axis["left"].label.set_text(r"z")
    ax1.axis["top"].label.set_text(r"RA")

    # create a parasite axes whose transData in RA, cz
    aux_ax = ax1.get_aux_axes(tr)

    aux_ax.patch = ax1.patch  
    ax1.patch.zorder = 0.9  

    return ax1, aux_ax

fig = plt.figure()

ax3, aux_ax3 = setup_axes3(fig, 111)

theta = np.linspace(0,np.pi/2.)
radius = np.linspace(0,1) 
aux_ax3.scatter(theta, radius)

plt.show()

除此之外,还不清楚问题是什么。

【讨论】:

  • 谢谢!这正是我需要知道的。我对 HMS 角度到底在做什么或如何为我的数据正确定义它们有些困惑。所以感谢你大大简化了我的问题。虽然我仍然对方向有一些问题。我的出来是向下旋转并仍然向左旋转。
  • 我建议不要使用您的实际数据,而是尝试使用示例。使用不同的假数据,使用不同的尺度,重新引入Affine2D() 转换并观察发生了什么。
猜你喜欢
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 2016-11-22
  • 2017-12-17
  • 1970-01-01
相关资源
最近更新 更多