pythonmatplotlibplotastronomysector

Plotting sector of polar plot (so a wedge) in matplotlib


So I'm trying to generate a plot of Right Ascension (RA) as a function of redshift (z) in a polar plot, but I only need one sector of the polar plot, so basically I just need a wedge (the last plot on the right in the image at the bottom is what I'm going for). I've been using matplotlib's example code demo_floating_axes.py but am still having some issues. I've been able to load in my data and get it to plot, but I'm not familiar enough with the example code to be able to tweak the things I need tweaked (particularly the orientation of the wedge and the labeling on the RA axis. Here is what I've been using. The majority is from the maplotlib example code.

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

I got an output which is kind of what I want (apparently I can't embed images yet so I can't include a pic of the output I'm getting but like I said, I'm trying to get something similar to the last plot on the right in the example image below), but for some reason its rotating the wedge by 90 degrees and its converting my RA degrees into sexagesimal which I do not want but I can't figure out where its converting it in the code. So if anyone can lend any help with this I would be super appreciative! Also, I will eventually need to move into 3D using RA and Dec as a function of redshift, so if anyone has any pointers on how I can make this into 3d that'd be awesome too!

matplotlib example pic


Solution

  • You may start off with a much simpler version without HMS angles.

    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

    Apart from that it's not really clear what the question is.