Пример #1
0
def create_marginalized_hist(ax, param, samples, percentiles=None, label=None,
        color='navy', filled=False, linecolor='b', title=True):
    """Plots a 1D marginalized histogram of the given param from the given
    samples.

    Parameters
    ----------
    ax : pyplot.Axes
        The axes on which to draw the plot.
    param : string
        The parameter to plot.
    samples : FieldArray
        A field array of the samples to plot.
    percentiles : {None, float or array}
        What percentiles to draw lines at. If None, will draw lines at
        `[5, 50, 95]` (i.e., the bounds on the upper 90th percentile and the
        median).
    label : {None, string}
        A label for the parameter. If None, will use `param`.
    color : {'navy', string}
        What color to make the histogram; default is navy.
    filled : {False, bool}
        Whether to fill the histogram; default is False.
    linecolor : {'b', string}
        What color to use for the percentile lines.
    title : {True, bool}
        Add a title with the median value +/- uncertainty, with the
        max(min) `percentile` used for the +(-) uncertainty.
    """
    if label is None:
        label = param
    if filled:
        htype = 'stepfilled'
    else:
        htype = 'step'
    values = samples[param]
    ax.hist(values, bins=50, color=color, histtype=htype)
    if percentiles is None:
        percentiles = [5., 50., 95.]
    values = numpy.percentile(values, percentiles)
    for val in values:
        ax.axvline(x=val, ls='dashed', color=linecolor)
    if title:
        values_med = numpy.median(values)
        values_min = values.min()
        values_max = values.max()
        negerror = values_med - values_min
        poserror = values_max - values_med
        fmt = '$' + str_utils.format_value(values_med, negerror,
              plus_error=poserror, ndecs=2) + '$'
        ax.set_title('{} = {}'.format(label, fmt))
    # Remove y-ticks
    ax.set_yticks([])
Пример #2
0
def create_marginalized_hist(ax, values, label, percentiles=None,
        color='k', fillcolor='gray', linecolor='navy', title=True,
        rotated=False, plot_min=None, plot_max=None):
    """Plots a 1D marginalized histogram of the given param from the given
    samples.

    Parameters
    ----------
    ax : pyplot.Axes
        The axes on which to draw the plot.
    values : array
        The parameter values to plot.
    label : str
        A label to use for the title.
    percentiles : {None, float or array}
        What percentiles to draw lines at. If None, will draw lines at
        `[5, 50, 95]` (i.e., the bounds on the upper 90th percentile and the
        median).
    color : {'k', string}
        What color to make the histogram; default is black.
    fillcolor : {'gray', string, or None}
        What color to fill the histogram with. Set to None to not fill the
        histogram. Default is 'gray'.
    linecolor : {'navy', string}
        What color to use for the percentile lines. Default is 'navy'.
    title : {True, bool}
        Add a title with the median value +/- uncertainty, with the
        max(min) `percentile` used for the +(-) uncertainty.
    rotated : {False, bool}
        Plot the histogram on the y-axis instead of the x. Default is False.
    plot_min : {None, float}
        The minimum value to plot. If None, will default to whatever `pyplot`
        creates.
    plot_max : {None, float}
        The maximum value to plot. If None, will default to whatever `pyplot`
        creates.
    scalefac : {1., float}
        Factor to scale the default font sizes by. Default is 1 (no scaling).
    """
    if fillcolor is None:
        htype = 'step'
    else:
        htype = 'stepfilled'
    if rotated:
        orientation = 'horizontal'
    else:
        orientation = 'vertical'
    ax.hist(values, bins=50, histtype=htype, orientation=orientation,
            facecolor=fillcolor, edgecolor=color, lw=2)
    if percentiles is None:
        percentiles = [5., 50., 95.]
    values = numpy.percentile(values, percentiles)
    for val in values:
        if rotated:
            ax.axhline(y=val, ls='dashed', color=linecolor, lw=2)
        else:
            ax.axvline(x=val, ls='dashed', color=linecolor, lw=2)
    if title:
        values_med = numpy.median(values)
        values_min = values.min()
        values_max = values.max()
        negerror = values_med - values_min
        poserror = values_max - values_med
        fmt = '$' + str_utils.format_value(values_med, negerror,
              plus_error=poserror, ndecs=2) + '$'
        if rotated:
            ax.yaxis.set_label_position("right")
            ax.set_ylabel('{} = {}'.format(label, fmt), rotation=-90,
                labelpad=26, fontsize=18)
            # Remove x-ticks
            ax.set_xticks([])
            # turn off x-labels
            ax.set_xlabel('')
            # set limits
            ymin, ymax = ax.get_ylim()
            if plot_min is not None:
                ymin = plot_min
            if plot_max is not None:
                ymax = plot_max
            ax.set_ylim(ymin, ymax)
        else:
            ax.set_title('{} = {}'.format(label, fmt), fontsize=18, y=1.04)
            # Remove y-ticks
            ax.set_yticks([])
            # turn off y-label
            ax.set_ylabel('')
            # set limits
            xmin, xmax = ax.get_xlim()
            if plot_min is not None:
                xmin = plot_min
            if plot_max is not None:
                xmax = plot_max
            ax.set_xlim(xmin, xmax)
Пример #3
0
def create_marginalized_hist(ax,
                             values,
                             label,
                             percentiles=None,
                             color='k',
                             fillcolor='gray',
                             linecolor='navy',
                             title=True,
                             expected_value=None,
                             expected_color='red',
                             rotated=False,
                             plot_min=None,
                             plot_max=None):
    """Plots a 1D marginalized histogram of the given param from the given
    samples.

    Parameters
    ----------
    ax : pyplot.Axes
        The axes on which to draw the plot.
    values : array
        The parameter values to plot.
    label : str
        A label to use for the title.
    percentiles : {None, float or array}
        What percentiles to draw lines at. If None, will draw lines at
        `[5, 50, 95]` (i.e., the bounds on the upper 90th percentile and the
        median).
    color : {'k', string}
        What color to make the histogram; default is black.
    fillcolor : {'gray', string, or None}
        What color to fill the histogram with. Set to None to not fill the
        histogram. Default is 'gray'.
    linecolor : {'navy', string}
        What color to use for the percentile lines. Default is 'navy'.
    title : {True, bool}
        Add a title with the median value +/- uncertainty, with the
        max(min) `percentile` used for the +(-) uncertainty.
    rotated : {False, bool}
        Plot the histogram on the y-axis instead of the x. Default is False.
    plot_min : {None, float}
        The minimum value to plot. If None, will default to whatever `pyplot`
        creates.
    plot_max : {None, float}
        The maximum value to plot. If None, will default to whatever `pyplot`
        creates.
    scalefac : {1., float}
        Factor to scale the default font sizes by. Default is 1 (no scaling).
    """
    if fillcolor is None:
        htype = 'step'
    else:
        htype = 'stepfilled'
    if rotated:
        orientation = 'horizontal'
    else:
        orientation = 'vertical'
    ax.hist(values,
            bins=50,
            histtype=htype,
            orientation=orientation,
            facecolor=fillcolor,
            edgecolor=color,
            lw=2,
            density=True)
    if percentiles is None:
        percentiles = [5., 50., 95.]
    values = numpy.percentile(values, percentiles)
    for val in values:
        if rotated:
            ax.axhline(y=val, ls='dashed', color=linecolor, lw=2, zorder=3)
        else:
            ax.axvline(x=val, ls='dashed', color=linecolor, lw=2, zorder=3)
    # plot expected
    if expected_value is not None:
        if rotated:
            ax.axhline(expected_value, color=expected_color, lw=1.5, zorder=2)
        else:
            ax.axvline(expected_value, color=expected_color, lw=1.5, zorder=2)
    if title:
        values_med = numpy.median(values)
        values_min = values.min()
        values_max = values.max()
        negerror = values_med - values_min
        poserror = values_max - values_med
        fmt = '${0}$'.format(
            str_utils.format_value(values_med,
                                   negerror,
                                   plus_error=poserror,
                                   ndecs=2))

        if rotated:
            ax.yaxis.set_label_position("right")

            # sets colored title for marginal histogram
            set_marginal_histogram_title(ax,
                                         fmt,
                                         color,
                                         label=label,
                                         rotated=rotated)

            # Remove x-ticks
            ax.set_xticks([])
            # turn off x-labels
            ax.set_xlabel('')
            # set limits
            ymin, ymax = ax.get_ylim()
            if plot_min is not None:
                ymin = plot_min
            if plot_max is not None:
                ymax = plot_max
            ax.set_ylim(ymin, ymax)

        else:

            # sets colored title for marginal histogram
            set_marginal_histogram_title(ax, fmt, color, label=label)

            # Remove y-ticks
            ax.set_yticks([])
            # turn off y-label
            ax.set_ylabel('')
            # set limits
            xmin, xmax = ax.get_xlim()
            if plot_min is not None:
                xmin = plot_min
            if plot_max is not None:
                xmax = plot_max
            ax.set_xlim(xmin, xmax)
Пример #4
0
def create_marginalized_hist(ax, values, label, percentiles=None,
                             color='k', fillcolor='gray', linecolor='navy',
                             linestyle='-',
                             title=True, expected_value=None,
                             expected_color='red', rotated=False,
                             plot_min=None, plot_max=None):
    """Plots a 1D marginalized histogram of the given param from the given
    samples.

    Parameters
    ----------
    ax : pyplot.Axes
        The axes on which to draw the plot.
    values : array
        The parameter values to plot.
    label : str
        A label to use for the title.
    percentiles : {None, float or array}
        What percentiles to draw lines at. If None, will draw lines at
        `[5, 50, 95]` (i.e., the bounds on the upper 90th percentile and the
        median).
    color : {'k', string}
        What color to make the histogram; default is black.
    fillcolor : {'gray', string, or None}
        What color to fill the histogram with. Set to None to not fill the
        histogram. Default is 'gray'.
    linestyle : str, optional
        What line style to use for the histogram. Default is '-'.
    linecolor : {'navy', string}
        What color to use for the percentile lines. Default is 'navy'.
    title : bool, optional
        Add a title with a estimated value +/- uncertainty. The estimated value
        is the pecentile halfway between the max/min of ``percentiles``, while
        the uncertainty is given by the max/min of the ``percentiles``. If no
        percentiles are specified, defaults to quoting the median +/- 95/5
        percentiles.
    rotated : {False, bool}
        Plot the histogram on the y-axis instead of the x. Default is False.
    plot_min : {None, float}
        The minimum value to plot. If None, will default to whatever `pyplot`
        creates.
    plot_max : {None, float}
        The maximum value to plot. If None, will default to whatever `pyplot`
        creates.
    scalefac : {1., float}
        Factor to scale the default font sizes by. Default is 1 (no scaling).
    """
    if fillcolor is None:
        htype = 'step'
    else:
        htype = 'stepfilled'
    if rotated:
        orientation = 'horizontal'
    else:
        orientation = 'vertical'
    ax.hist(values, bins=50, histtype=htype, orientation=orientation,
            facecolor=fillcolor, edgecolor=color, ls=linestyle, lw=2,
            density=True)
    if percentiles is None:
        percentiles = [5., 50., 95.]
    if len(percentiles) > 0:
        plotp = numpy.percentile(values, percentiles)
    else:
        plotp = []
    for val in plotp:
        if rotated:
            ax.axhline(y=val, ls='dashed', color=linecolor, lw=2, zorder=3)
        else:
            ax.axvline(x=val, ls='dashed', color=linecolor, lw=2, zorder=3)
    # plot expected
    if expected_value is not None:
        if rotated:
            ax.axhline(expected_value, color=expected_color, lw=1.5, zorder=2)
        else:
            ax.axvline(expected_value, color=expected_color, lw=1.5, zorder=2)
    if title:
        if len(percentiles) > 0:
            minp = min(percentiles)
            maxp = max(percentiles)
            medp = (maxp + minp) / 2.
        else:
            minp = 5
            medp = 50
            maxp = 95
        values_min = numpy.percentile(values, minp)
        values_med = numpy.percentile(values, medp)
        values_max = numpy.percentile(values, maxp)
        negerror = values_med - values_min
        poserror = values_max - values_med
        fmt = '${0}$'.format(str_utils.format_value(
            values_med, negerror, plus_error=poserror))
        if rotated:
            ax.yaxis.set_label_position("right")
            # sets colored title for marginal histogram
            set_marginal_histogram_title(ax, fmt, color,
                                         label=label, rotated=rotated)
        else:
            # sets colored title for marginal histogram
            set_marginal_histogram_title(ax, fmt, color, label=label)
    # remove ticks and set limits
    if rotated:
        # Remove x-ticks
        ax.set_xticks([])
        # turn off x-labels
        ax.set_xlabel('')
        # set limits
        ymin, ymax = ax.get_ylim()
        if plot_min is not None:
            ymin = plot_min
        if plot_max is not None:
            ymax = plot_max
        ax.set_ylim(ymin, ymax)
    else:
        # Remove y-ticks
        ax.set_yticks([])
        # turn off y-label
        ax.set_ylabel('')
        # set limits
        xmin, xmax = ax.get_xlim()
        if plot_min is not None:
            xmin = plot_min
        if plot_max is not None:
            xmax = plot_max
        ax.set_xlim(xmin, xmax)
Пример #5
0
def create_marginalized_hist(ax, values, label, percentiles=None,
                             color='k', fillcolor='gray', linecolor='navy',
                             linestyle='-',
                             title=True, expected_value=None,
                             expected_color='red', rotated=False,
                             plot_min=None, plot_max=None):
    """Plots a 1D marginalized histogram of the given param from the given
    samples.

    Parameters
    ----------
    ax : pyplot.Axes
        The axes on which to draw the plot.
    values : array
        The parameter values to plot.
    label : str
        A label to use for the title.
    percentiles : {None, float or array}
        What percentiles to draw lines at. If None, will draw lines at
        `[5, 50, 95]` (i.e., the bounds on the upper 90th percentile and the
        median).
    color : {'k', string}
        What color to make the histogram; default is black.
    fillcolor : {'gray', string, or None}
        What color to fill the histogram with. Set to None to not fill the
        histogram. Default is 'gray'.
    linestyle : str, optional
        What line style to use for the histogram. Default is '-'.
    linecolor : {'navy', string}
        What color to use for the percentile lines. Default is 'navy'.
    title : bool, optional
        Add a title with a estimated value +/- uncertainty. The estimated value
        is the pecentile halfway between the max/min of ``percentiles``, while
        the uncertainty is given by the max/min of the ``percentiles``. If no
        percentiles are specified, defaults to quoting the median +/- 95/5
        percentiles.
    rotated : {False, bool}
        Plot the histogram on the y-axis instead of the x. Default is False.
    plot_min : {None, float}
        The minimum value to plot. If None, will default to whatever `pyplot`
        creates.
    plot_max : {None, float}
        The maximum value to plot. If None, will default to whatever `pyplot`
        creates.
    scalefac : {1., float}
        Factor to scale the default font sizes by. Default is 1 (no scaling).
    """
    if fillcolor is None:
        htype = 'step'
    else:
        htype = 'stepfilled'
    if rotated:
        orientation = 'horizontal'
    else:
        orientation = 'vertical'
    ax.hist(values, bins=50, histtype=htype, orientation=orientation,
            facecolor=fillcolor, edgecolor=color, ls=linestyle, lw=2,
            density=True)
    if percentiles is None:
        percentiles = [5., 50., 95.]
    if len(percentiles) > 0:
        plotp = numpy.percentile(values, percentiles)
    else:
        plotp = []
    for val in plotp:
        if rotated:
            ax.axhline(y=val, ls='dashed', color=linecolor, lw=2, zorder=3)
        else:
            ax.axvline(x=val, ls='dashed', color=linecolor, lw=2, zorder=3)
    # plot expected
    if expected_value is not None:
        if rotated:
            ax.axhline(expected_value, color=expected_color, lw=1.5, zorder=2)
        else:
            ax.axvline(expected_value, color=expected_color, lw=1.5, zorder=2)
    if title:
        if len(percentiles) > 0:
            minp = min(percentiles)
            maxp = max(percentiles)
            medp = (maxp + minp) / 2.
        else:
            minp = 5
            medp = 50
            maxp = 95
        values_min = numpy.percentile(values, minp)
        values_med = numpy.percentile(values, medp)
        values_max = numpy.percentile(values, maxp)
        negerror = values_med - values_min
        poserror = values_max - values_med
        fmt = '${0}$'.format(str_utils.format_value(
            values_med, negerror, plus_error=poserror))
        if rotated:
            ax.yaxis.set_label_position("right")

            # sets colored title for marginal histogram
            set_marginal_histogram_title(ax, fmt, color,
                                         label=label, rotated=rotated)

            # Remove x-ticks
            ax.set_xticks([])
            # turn off x-labels
            ax.set_xlabel('')
            # set limits
            ymin, ymax = ax.get_ylim()
            if plot_min is not None:
                ymin = plot_min
            if plot_max is not None:
                ymax = plot_max
            ax.set_ylim(ymin, ymax)

        else:

            # sets colored title for marginal histogram
            set_marginal_histogram_title(ax, fmt, color, label=label)

            # Remove y-ticks
            ax.set_yticks([])
            # turn off y-label
            ax.set_ylabel('')
            # set limits
            xmin, xmax = ax.get_xlim()
            if plot_min is not None:
                xmin = plot_min
            if plot_max is not None:
                xmax = plot_max
            ax.set_xlim(xmin, xmax)