Python 在matplotlib中绘制极坐标图的扇区(因此为楔形)

Python 在matplotlib中绘制极坐标图的扇区(因此为楔形),python,matplotlib,plot,astronomy,sector,Python,Matplotlib,Plot,Astronomy,Sector,所以我试着在极坐标图中生成一个随红移(z)变化的赤经图,但我只需要极坐标图的一个扇区,所以基本上我只需要一个楔子(底部图像右侧的最后一个图就是我要的)。我一直在使用matplotlib的示例代码demo_floating_axes.py,但仍然存在一些问题。我已经能够加载数据并进行绘图,但我对示例代码还不够熟悉,无法调整我需要调整的内容(特别是楔块的方向和RA轴上的标签)。以下是我一直使用的内容。大部分来自maplotlib示例代码 from matplotlib.transforms impo

所以我试着在极坐标图中生成一个随红移(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度转换为sexagesimal,我不想这样,但我不知道它在代码中的转换位置。因此,如果有人能在这方面提供帮助,我将不胜感激!此外,我最终需要使用RA和Dec作为红移函数,进入3D,所以,如果有人对我如何将其制作成3d有任何建议,那也太棒了


您可以从一个没有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()


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

记号和记号标签是由定位器和格式化程序决定的,在这种情况下,
grid\u locator1
tick\u formatter1
用于角度轴。选择不同的记号可以得到不同的记号。不清楚“它旋转90度”是什么意思。您可以使用
Affine2D().translate
和参数
ra0,ra1
旋转并设置刻度。从这一点来看,问题确实不清楚,我认为无法回答。当我说它旋转了90度时,我的意思是与示例图相比。示例图中,点位于图的底部,圆形部分位于图的顶部图。当我使用上面的代码时,我得到了相反的方向,它更像是一个非常薄的薄片,而不是一个楔子,所以你不能真正看到点是如何分布的。至于标记器,我似乎找不到任何关于angle\u helper函数的文档,所以我不知道这行是什么terHMS()正在做。谢谢!这正是我需要知道的。我对HMS角度到底在做什么或如何为我的数据正确定义它们有些困惑。因此,谢谢你大大简化了我的问题。尽管我在方向上仍有一些问题。我的角度向下旋转,仍然向左旋转。我将d建议不要使用实际数据,而要使用示例。使用不同的假数据,使用不同的比例,重新引入
Affine2D()
变换,并观察发生了什么。