Пример #1
0
    ax.plot(1.0 / x, p92_comps(x), "k--", alpha=0.5)
    ax2.plot(1.0 / x, p92_comps(x), "k--", alpha=0.5)

    p92_comps = p92_fit.copy()
    p92_comps.FUV_amp_0 = 0.0
    p92_comps.NUV_amp_0 = 0.0
    p92_comps.SIL1_amp_0 = 0.0
    p92_comps.SIL2_amp_0 = 0.0
    ax.plot(1.0 / x, p92_comps(x), "k--", alpha=0.5)
    ax2.plot(1.0 / x, p92_comps(x), "k--", alpha=0.5)

    # finish configuring the plot
    ax.set_yscale("linear")
    ax.set_xscale("log")
    ax.set_xlabel(r"$\lambda$ [$\mu m$]", fontsize=1.3 * fontsize)
    ax.set_ylabel(extdata._get_ext_ytitle(extdata.type),
                  fontsize=1.3 * fontsize)
    ax.tick_params("both", length=10, width=2, which="major")
    ax.tick_params("both", length=5, width=1, which="minor")
    ax.legend()

    # finish configuring the subplot
    sp_xlim = [2.0, 35.0]
    ax2.set_xlim(sp_xlim)
    # ax2.set_ylim(-best_fit_Av-0.1, -best_fit_Av+0.5)
    (indxs, ) = np.where((x.value > 1.0 / sp_xlim[1])
                         & (x.value < 1.0 / sp_xlim[0]))
    if extdata.type == "elx":
        ax2.set_ylim(
            min([min(p92_fit(x)[indxs]), -best_fit_Av]) - 0.1,
            max(p92_fit(x)[indxs]) + 0.1)
Пример #2
0
def plot_extinction(
    starpair,
    path,
    alax=False,
    extmodels=False,
    fitmodel=False,
    HI_lines=False,
    range=None,
    exclude=[],
    log=False,
    pdf=False,
):
    """
    Plot the extinction curve of a star

    Parameters
    ----------
    starpair : string
        Name of the star pair for which to plot the extinction curve, in the format "reddenedstarname_comparisonstarname" (no spaces)

    path : string
        Path to the data files

    alax : boolean [default=False]
        Whether or not to plot A(lambda)/A(X) instead of E(lambda-X)

    extmodels: boolean [default=False]
        Whether or not to overplot Milky Way extinction curve models

    fitmodel: boolean [default=False]
        Whether or not to overplot a fitted model

    HI_lines : boolean [default=False]
        Whether or not to indicate the HI-lines in the plot

    range : list of 2 floats [default=None]
        Wavelength range to be plotted (in micron) - [min,max]

    exclude : list of strings [default=[]]
        List of data type(s) to exclude from the plot (e.g., IRS)

    log : boolean [default=False]
        Whether or not to plot the wavelengths on a log scale

    pdf : boolean [default=False]
        Whether or not to save the figure as a pdf file

    Returns
    -------
    Figure with extinction curve
    """
    # plotting setup for easier to read plots
    fontsize = 18
    font = {"size": fontsize}
    plt.rc("font", **font)
    plt.rc("lines", linewidth=1)
    plt.rc("axes", linewidth=2)
    plt.rc("xtick.major", width=2, size=10)
    plt.rc("xtick.minor", width=1, size=5)
    plt.rc("ytick.major", width=2, size=10)
    plt.rc("ytick.minor", width=1, size=5)
    plt.rc("axes.formatter", min_exponent=2)

    # create the plot
    fig, ax = plt.subplots(figsize=(13, 10))

    # read in and plot the extinction curve data for this star
    extdata = ExtData("%s%s_ext.fits" % (path, starpair.lower()))
    extdata.plot(ax, alax=alax, exclude=exclude, color="k")

    # define the output name
    outname = "%s_ext_%s.pdf" % (starpair.lower(), extdata.type)

    # plot Milky Way extinction models if requested
    if extmodels:
        plot_extmodels(extdata, alax)

    # overplot a fitted model if requested
    if fitmodel:
        plot_fitmodel(extdata, res=True)

    # plot HI-lines if requested
    if HI_lines:
        plot_HI(path, ax)

    # zoom in on a specific region if requested
    if range is not None:
        zoom(ax, range)
        outname = outname.replace(".pdf", "_zoom.pdf")

    # finish configuring the plot
    ax.set_title(starpair.split("_")[0], fontsize=50)
    ax.text(
        0.99,
        0.95,
        "comparison: " + starpair.split("_")[1],
        fontsize=25,
        horizontalalignment="right",
        transform=ax.transAxes,
    )
    if log:
        ax.set_xscale("log")
    plt.xlabel(r"$\lambda$ [$\mu m$]", fontsize=1.5 * fontsize)
    ax.set_ylabel(extdata._get_ext_ytitle(ytype=extdata.type),
                  fontsize=1.5 * fontsize)

    # show the figure or save it to a pdf file
    if pdf:
        fig.savefig(path + outname, bbox_inches="tight")
        plt.close()
    else:
        plt.show()
Пример #3
0
def plot_average(
    path,
    filename="average_ext.fits",
    ax=None,
    extmodels=False,
    fitmodel=False,
    HI_lines=False,
    range=None,
    exclude=[],
    log=False,
    spread=False,
    annotate_key=None,
    annotate_wave_range=None,
    pdf=False,
):
    """
    Plot the average extinction curve

    Parameters
    ----------
    path : string
        Path to the average extinction curve fits file

    filename : string [default="average_ext.fits"]
        Name of the average extinction curve fits file

    ax : AxesSubplot [default=None]
        Axes of plot on which to add the average extinction curve if pdf=False

    extmodels: boolean [default=False]
        Whether or not to overplot Milky Way extinction curve models

    fitmodel: boolean [default=False]
        Whether or not to overplot a fitted model

    HI_lines : boolean [default=False]
        Whether or not to indicate the HI-lines in the plot

    range : list of 2 floats [default=None]
        Wavelength range to be plotted (in micron) - [min,max]

    exclude : list of strings [default=[]]
        List of data type(s) to exclude from the plot (e.g., IRS)

    log : boolean [default=False]
        Whether or not to plot the wavelengths on a log-scale

    spread : boolean [default=False]
        Whether or not to offset the average extinction curve from the other curves (only relevant when pdf=False and ax=None)

    annotate_key : string [default=None]
        type of data for which to annotate text (e.g., SpeX_LXD) (only relevant when pdf=False and ax=None)

    annotate_wave_range : list of 2 floats [default=None]
        min/max wavelength range for the annotation of the text (only relevant when pdf=False and ax=None)

    pdf : boolean [default=False]
        - If False, the average extinction curve will be overplotted on the current plot (defined by ax)
        - If True, the average extinction curve will be plotted in a separate plot and saved as a pdf

    Returns
    -------
    Plots the average extinction curve
    """
    # read in the average extinction curve (if it exists)
    if os.path.isfile(path + filename):
        average = ExtData(path + filename)
    else:
        warnings.warn(
            "An average extinction curve with the name " + filename +
            " could not be found in " + path +
            ". Please calculate the average extinction curve first with the calc_ave_ext function in measure_extinction/utils/calc_ext.py.",
            UserWarning,
        )

    # make a new plot if requested
    if pdf:
        # plotting setup for easier to read plots
        fontsize = 18
        font = {"size": fontsize}
        plt.rc("font", **font)
        plt.rc("lines", linewidth=1)
        plt.rc("axes", linewidth=2)
        plt.rc("xtick.major", width=2, size=10)
        plt.rc("xtick.minor", width=1, size=5)
        plt.rc("ytick.major", width=2, size=10)
        plt.rc("ytick.minor", width=1, size=5)
        plt.rc("axes.formatter", min_exponent=2)

        # create the plot
        fig, ax = plt.subplots(figsize=(13, 10))
        average.plot(ax, exclude=exclude, color="k")

        # plot Milky Way extinction models if requested
        if extmodels:
            plot_extmodels(average, alax=True)

        # overplot a fitted model if requested
        if fitmodel:
            plot_fitmodel(average, res=True)

        # plot HI-lines if requested
        if HI_lines:
            plot_HI(path, ax)

        # zoom in on a specific region if requested
        if range is not None:
            zoom(ax, range)

        # finish configuring the plot
        if log:
            ax.set_xscale("log")
        plt.xlabel(r"$\lambda$ [$\mu m$]", fontsize=1.5 * fontsize)
        ax.set_ylabel(average._get_ext_ytitle(ytype=average.type),
                      fontsize=1.5 * fontsize)
        fig.savefig(path + "average_ext.pdf", bbox_inches="tight")

        # return the figure and axes for additional manipulations
        return fig, ax

    else:
        if spread:
            yoffset = -0.3
        else:
            yoffset = 0
        average.plot(
            ax,
            color="k",
            alpha=0.6,
            yoffset=yoffset,
            annotate_key=annotate_key,
            annotate_wave_range=annotate_wave_range,
            annotate_text="average",
            annotate_yoffset=0.05,
        )

        # overplot a fitted model if requested
        if fitmodel:
            plot_fitmodel(average, yoffset=yoffset)
Пример #4
0
def plot_multi_extinction(
    starpair_list,
    path,
    alax=False,
    average=False,
    extmodels=False,
    fitmodel=False,
    HI_lines=False,
    range=None,
    spread=False,
    exclude=[],
    log=False,
    text_offsets=[],
    text_angles=[],
    pdf=False,
):
    """
    Plot the extinction curves of multiple stars in the same plot

    Parameters
    ----------
    starpair_list : list of strings
        List of star pairs for which to plot the extinction curve, in the format "reddenedstarname_comparisonstarname" (no spaces)

    path : string
        Path to the data files

    alax : boolean [default=False]
        Whether or not to plot A(lambda)/A(X) instead of E(lambda-X)

    average : boolean [default=False]
        Whether or not to plot the average extinction curve

    extmodels: boolean [default=False]
        Whether or not to overplot Milky Way extinction curve models

    fitmodel: boolean [default=False]
        Whether or not to overplot a fitted model

    HI_lines : boolean [default=False]
        Whether or not to indicate the HI-lines in the plot

    range : list of 2 floats [default=None]
        Wavelength range to be plotted (in micron) - [min,max]

    spread : boolean [default=False]
        Whether or not to spread the extinction curves out by adding a vertical offset to each curve

    exclude : list of strings [default=[]]
        List of data type(s) to exclude from the plot (e.g., IRS)

    log : boolean [default=False]
        Whether or not to plot the wavelengths on a log-scale

    text_offsets : list of floats [default=[]]
        List of the same length as starpair_list with offsets for the annotated text

    text_angles : list of integers [default=[]]
        List of the same length as starpair_list with rotation angles for the annotated text

    pdf : boolean [default=False]
        Whether or not to save the figure as a pdf file

    Returns
    -------
    Figure with extinction curves of multiple stars
    """
    # plotting setup for easier to read plots
    fontsize = 18
    font = {"size": fontsize}
    plt.rc("font", **font)
    plt.rc("lines", linewidth=1)
    plt.rc("axes", linewidth=2)
    plt.rc("xtick.major", width=2, size=10)
    plt.rc("xtick.minor", width=1, size=5)
    plt.rc("ytick.major", width=2, size=10)
    plt.rc("ytick.minor", width=1, size=5)
    plt.rc("axes.formatter", min_exponent=2)

    # create the plot
    fig, ax = plt.subplots(figsize=(15, len(starpair_list) * 1.25))
    colors = plt.get_cmap("tab10")

    # set default text offsets and angles
    if text_offsets == []:
        text_offsets = np.full(len(starpair_list), 0.2)
    if text_angles == []:
        text_angles = np.full(len(starpair_list), 10)

    for i, starpair in enumerate(starpair_list):
        # read in the extinction curve data
        extdata = ExtData("%s%s_ext.fits" % (path, starpair.lower()))

        # spread out the curves if requested
        if spread:
            yoffset = 0.25 * i
        else:
            yoffset = 0.0

        # determine where to add the name of the star
        # find the shortest plotted wavelength
        (waves, exts,
         ext_uncs) = extdata.get_fitdata(extdata.waves.keys() - exclude)
        if range is not None:
            waves = waves[waves.value >= range[0]]
        min_wave = waves[-1]
        # find out which data type corresponds with this wavelength
        for data_type in extdata.waves.keys():
            if data_type in exclude:
                continue
            used_waves = extdata.waves[data_type][extdata.npts[data_type] > 0]
            if min_wave in used_waves:
                ann_key = data_type
        ann_range = [min_wave, min_wave] * u.micron

        # plot the extinction curve
        extdata.plot(
            ax,
            color=colors(i % 10),
            alpha=0.7,
            alax=alax,
            exclude=exclude,
            yoffset=yoffset,
            annotate_key=ann_key,
            annotate_wave_range=ann_range,
            annotate_text=extdata.red_file.split(".")[0].upper(),
            annotate_yoffset=text_offsets[i],
            annotate_rotation=text_angles[i],
            annotate_color=colors(i % 10),
        )

        # overplot a fitted model if requested
        if fitmodel:
            plot_fitmodel(extdata, yoffset=yoffset)

    # overplot Milky Way extinction curve models if requested
    if extmodels:
        if alax:
            plot_extmodels(extdata, alax)
        else:
            warnings.warn(
                "Overplotting Milky Way extinction curve models on a figure with multiple observed extinction curves in E(lambda-V) units is disabled, because the model curves in these units are different for every star, and would overload the plot. Please, do one of the following if you want to overplot Milky Way extinction curve models: 1) Use the flag --alax to plot ALL curves in A(lambda)/A(V) units, OR 2) Plot all curves separately by removing the flag --onefig.",
                stacklevel=2,
            )

    # plot the average extinction curve if requested
    if average:
        plot_average(
            path,
            ax=ax,
            extmodels=extmodels,
            fitmodel=fitmodel,
            exclude=exclude,
            spread=spread,
            annotate_key=ann_key,
            annotate_wave_range=ann_range,
        )

    # define the output name
    outname = "all_ext_%s.pdf" % (extdata.type)

    # plot HI-lines if requested
    if HI_lines:
        plot_HI(path, ax)

    # zoom in on a specific region if requested
    if range is not None:
        zoom(ax, range)
        outname = outname.replace(".pdf", "_zoom.pdf")

    # finish configuring the plot
    if log:
        ax.set_xscale("log")
    ax.set_xlabel(r"$\lambda$ [$\mu m$]", fontsize=1.5 * fontsize)
    ylabel = extdata._get_ext_ytitle(ytype=extdata.type)
    if spread:
        ylabel += " + offset"
    ax.set_ylabel(ylabel, fontsize=1.5 * fontsize)

    # show the figure or save it to a pdf file
    if pdf:
        fig.savefig(path + outname, bbox_inches="tight")
    else:
        plt.show()

    # return the figure and axes for additional manipulations
    return fig, ax
Пример #5
0
    extdata.plot_ext(ax, alav=args.alav)

    # fix the x,y plot limits
    # ax.set_xlim(ax.get_xlim())
    # ax.set_xlim(0.1, 2.5)
    ax.set_ylim(ax.get_ylim())

    # finish configuring the plot
    ax.set_yscale('linear')
    ax.set_xscale('log')
    ax.set_xlabel('$\lambda$ [$\mu m$]', fontsize=1.3 * fontsize)
    if args.alav:
        ytype = 'alav'
    else:
        ytype = 'elv'
    ax.set_ylabel(extdata._get_ext_ytitle(ytype), fontsize=1.3 * fontsize)
    ax.tick_params('both', length=10, width=2, which='major')
    ax.tick_params('both', length=5, width=1, which='minor')
    ax.set_title(args.extfile)

    # plot extinctionm models
    if args.extmodels:
        x = np.arange(0.12, 3.0, 0.01) * u.micron
        Rvs = [2.0, 3.1, 4.0, 5.0]
        for cRv in Rvs:
            t = CCM89(Rv=cRv)
            ax.plot(x,
                    t(x),
                    'k--',
                    linewidth=2,
                    label='R(V) = {:4.2f}'.format(cRv))