绘制matplotlib中

时间:2017-05-25 20:10:30

标签: python matplotlib plot astronomy sector

所以我试图在极坐标图中生成一个右上升(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()

我得到了一个输出,这是我想要的东西(显然我不能嵌入图像,所以我不能包括我得到的输出图片,但就像我说的,我和#39;我试图得到类似于下面示例图片中右边最后一个情节的东西),但由于某种原因,它将楔形旋转了90度,并将我的RA度转换为六十进制,我不想要,但我可以& #39;弄清楚它在代码中的转换位置。因此,如果任何人都可以提供任何帮助,我将非常感激!另外,我最终需要使用RA和Dec作为红移的功能进入3D,所以如果有人有任何关于如何将其变成3D的指示,那也是非常棒的!

matplotlib example pic

1 个答案:

答案 0 :(得分: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()

enter image description here

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