Exemplo n.º 1
0
def plot_spatial_closeup(gc_orb, gc_stream):
    """plot_spatial_closeup.

    TODO
    ----
    make general

    """
    fig, axs = plt.subplots(1, 3, figsize=(15, 5))
    qpairs = [("x", "y"), ("x", "z"), ("y", "z")]
    q1lims = [(8, 8.4), (8, 8.4), (0, 0.5)]
    q2lims = [(-0.5, 1), (16.2, 16.8), (16.2, 16.8)]

    for ax, (q1, q2), q1lim, q2lim in zip(axs, qpairs, q1lims, q2lims):
        # Stream
        dd1 = getattr(gc_stream.cartesian, q1).value
        dd2 = getattr(gc_stream.cartesian, q2).value
        _ind = ((q1lim[0] < dd1)
                & (dd1 < q1lim[1])
                & (q2lim[0] < dd2)
                & (dd2 < q2lim[1]))

        plt.sca(ax)
        # TODO switch to _scatterhelp
        plt.scatter(
            getattr(gc_stream.cartesian, q1)[_ind],
            getattr(gc_stream.cartesian, q2)[_ind],
            s=2,
            label="stream",
        )
        # Pal5 Integrated
        plt.scatter(
            getattr(gc_orb.cartesian, q1),
            getattr(gc_orb.cartesian, q2),
            s=1,
            c="r",
            label="Pal5",
        )
        # Progenitor
        plt.scatter(
            getattr(gc_orb.cartesian, q1)[0],
            getattr(gc_orb.cartesian, q2)[0],
            s=30,
            c="r",
            edgecolor="k",
            label="Pal5",
        )

        plt.set(
            title="{q1} & {q2} (Galactocentric)".format(q1=q1, q2=q2),
            xlabel=q1,
            ylabel=q2,
            xlim=q1lim,
            ylim=q2lim,
        )
    fig.tight_layout()
    return fig
Exemplo n.º 2
0
def plot_radec_track_residual(data_radec, track_radec, track_interp, **kw):
    """plot_radec_track_residual.

    calculates residual as (dec - track_interp.dec) / dec_err

    Parameters
    ----------
    data_radec : (n, 3) array
        measured data.
        columns are [ra, dec, dec_err]
    track_radec :
    track_interp : function

    Returns
    -------
    fig : matplotlib figure
    (frame1, frame2) : matplotlib axes

    """
    # Preplotting
    ra, dec, dec_err = data_radec.T

    residual = dec - track_interp(ra)

    xmin, xmax = min(ra), max(ra)
    xlim = [xmin - 2.5, xmax + 2.5]

    ymin, ymax = min(dec), max(dec)
    ylim = [ymin - 2.5, ymax + 2.5]

    # Plotting
    fig = plt.figure()
    # main plot
    frame1 = fig.add_axes((0.1, 0.3, 0.8, 0.6))
    plt.errorbar(ra, dec, yerr=dec_err, fmt=".r", zorder=0, label="data")
    plt.plot(track_radec[:, 0], track_radec[:, 1], label="track")
    plt.scatter(ra, track_interp(ra), s=10, c="k", label="match")
    frame1.set_xticklabels([])
    frame1.set(xlim=xlim, ylim=ylim, ylabel="Dec [degree]")
    plt.grid(True)

    # residual plot
    frame2 = fig.add_axes((0.1, 0.1, 0.8, 0.2))
    plt.axhline(0, c="tab:blue")
    plt.plot(ra, residual, "or")
    frame2.set(xlim=xlim, ylim=ylim, xlabel="RA [degree]")
    plt.grid(False)

    plt.set(**kw, ax=frame1)

    return fig, (frame1, frame2)
Exemplo n.º 3
0
def plot_data_along_orbit(data,
                          orbit,
                          idxres,
                          frame="cust",
                          lon_angle="phi1",
                          lat_angle="phi2",
                          **kw):
    """plot_data_along_orbit."""
    # --------------------

    if isinstance(data, SkyCoord):
        dataphi1 = getattr(data, lon_angle)[idxres.idxcatalog]
        dataphi2 = getattr(data, lat_angle)[idxres.idxcatalog]
    elif isinstance(data, (Table, QTable)):
        dataphi1 = data[lon_angle][idxres.idxcatalog]
        dataphi2 = data[lat_angle][idxres.idxcatalog]
    else:
        raise TypeError("data is not a SkyCoord or (Q)Table")

    # --------------------

    if isinstance(orbit, SkyCoord):
        orbitphi1 = getattr(orbit, lon_angle)[idxres.idxo]
        orbitphi2 = getattr(orbit, lat_angle)[idxres.idxo]
    elif isinstance(orbit, (Table, QTable)):
        orbitphi1 = orbit[lon_angle][idxres.idxo]
        orbitphi2 = orbit[lat_angle][idxres.idxo]
    else:
        raise TypeError("orbit is not a SkyCoord or (Q)Table")

    # --------------------

    # orbit
    plt.scatter(orbitphi1, orbitphi2, c="k", s=0.1)
    # data
    plt.scatter(dataphi1, dataphi2, s=0.1)
    # setting plot properties
    plt.set(
        aspect="equal",
        title=kw.pop("title", "Data Along Orbit"),
        xlabel=kw.pop("xlabel", rf"{lon_angle} [deg]"),
        ylabel=kw.pop("ylabel", rf"{lat_angle} [deg]"),
    )
    plt.set(**kw)  # any extra user keywords

    return
Exemplo n.º 4
0
def plot_data_along_orbit_in_window(
    data,
    orbit,
    idxres,
    windowcut=...,
    frame="cust",
    lon_angle="phi1",
    lat_angle="phi2",
    **kw,
):
    """plot_data_along_orbit_in_window.

    Parameters
    ----------
    data : SkyCoord
    orbit : SkyCoord

    """
    # --------------------

    if isinstance(data, SkyCoord):
        phi1 = getattr(data, lon_angle)[idxres.idxcatalog]
        phi2 = getattr(data, lat_angle)[idxres.idxcatalog]
    elif isinstance(data, (Table, QTable)):
        phi1 = data[lon_angle][idxres.idxcatalog]
        phi2 = data[lat_angle][idxres.idxcatalog]
    else:
        raise TypeError("data is not a SkyCoord or (Q)Table")

    # --------------------

    plot_sky_window(
        data,
        ind=...,
        orbrng=windowcut,
        fig=None,
        frame=frame,
        lon_angle=lon_angle,
        lat_angle=lat_angle,
    )
    plt.scatter(phi1, phi2, s=40, c="k", alpha=0.05)
    plt.set(**kw)

    return
Exemplo n.º 5
0
def plot_spatial(gal_stream, gal_orb):
    """plot_spatial."""
    fig, axs = plt.subplots(1, 3, figsize=(15, 5))
    for ax, (q1, q2) in zip(axs, (("x", "y"), ("x", "z"), ("y", "z"))):
        s1_arr = getattr(gal_stream.cartesian, q1)[::50]
        s2_arr = getattr(gal_stream.cartesian, q2)[::50]
        ax.scatter(s1_arr, s2_arr, s=2, label="stream")

        p1_arr = getattr(gal_orb.cartesian, q1)
        p2_arr = getattr(gal_orb.cartesian, q2)
        ax.scatter(p1_arr, p2_arr, s=2, c="r", label="progenitor")

        plt.set(
            ax=ax,
            title="{q1} & {q2} (Galactic)".format(q1=q1, q2=q2),
            xlabel=r"{q1} [{unit}]".format(q1=q1, unit=s1_arr.unit),
            ylabel=r"{q2} [{unit}]".format(q2=q2, unit=s2_arr.unit),
        )
    fig.tight_layout()

    return fig
Exemplo n.º 6
0
def plot_data_orbit_projection(
    data,
    orbit,
    idxres,
    skycut=Ellipsis,
    filtered=True,
    step=0.07,
    sigma=(2, 2),
    add_sidehists=True,
    frame="cust",
    lon_angle="phi1",
    lat_angle="phi2",
    **kw,
):
    """plot_data_orbit_projection.

    Parameters
    ----------
    data : SkyCoord, (Q)Table
    orbit : SkyCoord, (Q)Table
    idxres :
        returned by get_data_along_orbit
    skycut : index array, optional  (default Ellipsis)
    filtered : bool, optional  (default True)
        whether to apply a gaussian filter
    step : float, optional  (default .07)
        the step size in the gaussian filter
    sigma : list, optional  (default (2, 2))
        the sigma in the guassian filter

    Returns
    -------
    im : matplotlib plot
        if not filtered:
            scatter(*, s=2, sidehists=True)
        if filtered:
            imshow(*, cmap='Spectral', norm=colors.LogNorm(),
                   interpolation='nearest')

    """
    # --------------------
    if idxres is None:  # assumed already used idxcatalog & idxo
        idxcatalog = np.ones(len(data), dtype=bool)
        idxo = np.ones(len(data), dtype=bool)
    else:
        idxcatalog = idxres.idxcatalog
        idxo = idxres.idxo

    # --------------------

    if isinstance(data, SkyCoord):
        # dataphi1 = getattr(data, lon_angle)[idxcatalog]
        dataphi2 = getattr(data, lat_angle)[idxcatalog]
    elif isinstance(data, (Table, QTable)):
        # dataphi1 = data[lon_angle][idxcatalog]
        dataphi2 = data[lat_angle][idxcatalog]
    else:
        raise TypeError("data is not a SkyCoord or (Q)Table")

    # --------------------

    if isinstance(orbit, SkyCoord):
        # orbphi1 = getattr(orbit, lon_angle)
        orbphi2 = getattr(orbit, lat_angle)
    elif isinstance(orbit, (Table, QTable)):
        # orbphi1 = orbit[lon_angle]
        orbphi2 = orbit[lat_angle]
    else:
        raise TypeError("orbit is not a SkyCoord or (Q)Table")

    # --------------------

    # distance along the arc
    x = idxres.arc[idxo][skycut].to_value("deg")

    # perpendicular to arc
    y = idxres.wsky.copy()
    y[dataphi2 < orbphi2[idxo]] *= -1
    y = y[skycut].to_value("deg")

    # --------------------

    plt.grid(False)
    ax = plt.gca()

    if not filtered:
        im = plt.scatter(x, y, s=2, sidehists=add_sidehists)
    else:
        xbins = np.arange(x.min(), x.max() + step, step)
        ybins = np.arange(y.min(), y.max() + step, step)

        H, xedges, yedges = np.histogram2d(x,
                                           y,
                                           bins=[xbins, ybins],
                                           normed=True)
        H = np.rot90(H)
        extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

        fH = gaussian_filter(H, sigma=sigma, order=0)

        im = plt.imshow(
            fH,
            extent=extent,
            cmap="Spectral",
            norm=colors.LogNorm(),
            interpolation="nearest",
            xlabel=r"$s_1$",
            ylabel=r"$s_2$",
        )

        shargs = {k: v for k, v in kw.items() if k.startswith("sh")}
        [kw.pop(k) for k in shargs.keys()]
        plt.scatter(x, y, alpha=0, sidehists=add_sidehists, **shargs)

    axs = plt.gcf().axes
    axs[0].set_aspect("equal")
    axs[0].tick_params(axis="both",
                       labelsize=13,
                       color="black",
                       colors="black")
    axs[1].tick_params(axis="both", labelsize=12)
    axs[2].tick_params(axis="both", labelsize=12)
    axs[0].set_xlabel(r"$s_1 \ (\rm{deg})$", fontsize=20)
    axs[0].set_ylabel(r"$s_2 \ (\rm{deg})$", fontsize=20)
    if not isinstance(add_sidehists, bool):
        if "left" in add_sidehists:
            axs[0].yaxis.set_label_position("right")

    plt.set(figsize=(8, 3.5))
    plt.tight_layout()

    plt.set(**kw)
    plt.sca(ax)

    return im