def make_histogram_plots(opt):
    alpha_mean = .2
    alpha_hist = .6
    title = ""
    ps.set_style('standard')
    opt = tripcor.check_opt(opt)
    cwd = get_data_output_folder(opt.cwd)
    output_folder = get_plot_output_folder(opt.cwd)
    for beam, xing, error_types, error_loc, optic_type in hist_loop(opt):
        fig, axs = plt.subplots(len(AMPDET_NAMES), 1)
        for idx_data, output_id in _ordered_output_ids(opt.machine, beam,
                                                       opt.unused_stages):
            title, filename = get_seed_data_title_and_filename(
                beam, xing, error_types, error_loc, optic_type, output_id)
            seed_df = tfs.read_tfs(os.path.join(cwd, filename), index="SEED")
            y_max = len(seed_df.index)
            for idx_ax, term in enumerate(AMPDET_NAMES):
                ax = axs[idx_ax]
                data = seed_df[term]
                x_pos = data.mean()

                # plot mean
                stem_cont = ax.stem([x_pos], [y_max],
                                    markerfmt="",
                                    basefmt="",
                                    label="_nolegend_")
                plt.setp(stem_cont[1],
                         color=ps.get_mpl_color(idx_data),
                         alpha=alpha_mean)

                # plot std
                # error = data.std()
                # ebar_cont = ax.errorbar(x_pos, y_max, xerr=error, color=ps.get_mpl_color(idx_data),
                #                     label="_nolegend_", marker="")
                # ps.change_ebar_alpha_for_line(ebar_cont, alpha_mean)

                # plot histogram
                data.hist(ax=ax,
                          alpha=alpha_hist,
                          color=ps.get_mpl_color(idx_data),
                          label=output_id)

        for idx_ax, term in enumerate(AMPDET_NAMES):
            axs[idx_ax].set_xlabel(term)
            axs[idx_ax].set_ylabel("Seeds")

        legend = axs[0].legend()
        _reorder_legend(
            legend, _get_all_output_ids(opt.machine, beam, opt.unused_stages))
        ps.sync2d(axs)
        title = " ".join(title.split(" ")[:-1])
        figfilename = "{:s}.{:s}".format(title.replace(' ', '.'), "histogram")
        fig.canvas.set_window_title(title)
        make_bottom_text(axs[-1], title)
        fig.savefig(os.path.join(output_folder, figfilename + ".pdf"))
        fig.savefig(os.path.join(output_folder, figfilename + ".png"))
def add_measurement_lines(ax, current_data, lines):
    """ Add horizontal lines to represent measurement data. """
    acd_corr = 1 if current_data["ampdet"] in ["ANHX0100", "ANHY1000"] else .5
    for line in lines:
        line = line.replace("_in_IP5_IP1", "")
        id = "_".join([
            "b{:d}".format(current_data["beam"]), line, current_data["ampdet"]
        ])
        try:
            measdata = measurement_dict[id]
        except KeyError:
            pass
        else:
            low = (measdata[0] - measdata[1]) * acd_corr * 1e-4
            upp = (measdata[0] + measdata[1]) * acd_corr * 1e-4
            color = colors.to_rgba(ps.get_mpl_color(color_index.index(line)),
                                   .3)
            ax.fill_between([0, len(PLOT_STAGE_ORDER)],
                            2 * [upp],
                            2 * [low],
                            facecolor=color)
            ax.plot([0, len(PLOT_STAGE_ORDER)],
                    2 * [measdata[0] * acd_corr * 1e-4],
                    '--',
                    color=color[0:3])
def _plot_all_ampdets_in_one(cwd, data, current_params):
    """ Plots all ampdet terms into one plot """
    title = get_plot_title(current_params)
    output_file = os.path.join(cwd, title.replace(" ", ".") + ".allampdet")
    if title[0] == "b":
        title = "Beam" + title[1:]

    LOG.info("Writing plot for '{:s}'".format(title))
    lines = []
    color_cycle = ps.get_mpl_color()
    for ampdet in AMPDET_NAMES:
        current_color = color_cycle.next()
        lines += [
            go.Scatter(
                x=list(range(len(data.index))),
                y=list(data.loc[:, ampdet + "_AVG"]),
                error_y=dict(
                    array=list(data.loc[:, ampdet + "_STD"]),
                    color=current_color,
                    opacity=.5,
                ),
                mode='markers+lines',
                name=ampdet,
                hoverinfo="y+text",
                hovertext=list(data.index),
                line=dict(color=current_color),
            )
        ]

    xaxis = dict(range=[-0.1, len(data.index) - 0.9],
                 title=title,
                 showgrid=True,
                 ticks="outer",
                 ticktext=list(data.index),
                 tickvals=list(range(len(data.index))))

    yaxis = dict(title="Values  [$m^{-1}$] ", )
    layout = plotly_predef.get_layout(xaxis=xaxis, yaxis=yaxis)
    _plot_and_output(output_file, lines, layout, title)
コード例 #4
0
def plot_wrapper(data, layout, title, legend_cols=3, fig=None):
    tick_length_before_rotation = 6
    # create figure
    if fig is None:
        fig = plt.figure()
        gs = gridspec.GridSpec(1, 1, height_ratios=[1])
        ax = fig.add_subplot(gs[0])
    else:
        ax = fig.gca()
    fig.canvas.set_window_title(title)
    color_idx = 0
    n_legend = 0
    for idx, line in enumerate(data):
        e_val = None
        with suppress_exception(KeyError):
            e_val = line["error_y"]["array"]

        color = ps.get_mpl_color(color_idx)
        color_idx += 1
        with suppress_exception([KeyError, IndexError]):
            if line["line"]["color"]:
                color = ps.rgb_plotly_to_mpl(line["line"]["color"])
                color_idx += -1

        marker = ""
        if "marker" in line["mode"]:
            marker = ps.MarkerList.get_marker(idx)

        ls = ""
        if "lines" in line["mode"]:
            ls = rcParams[u"lines.linestyle"]

        label = line["name"]
        n_legend += 1
        with suppress_exception(KeyError):
            if line["showlegend"] is not None and not line["showlegend"]:
                ax.text(
                    x=line["x"][0],
                    y=line["y"][1] + (line["y"][1] - line["y"][0]) * 0.002,
                    s=label,
                    ha="center",
                    va="bottom",
                    color=color,
                )
                label = "_nolegend_"
                n_legend += -1

        _, caps, bars = ax.errorbar(
            line["x"],
            line["y"],
            yerr=e_val,
            ls=ls,
            marker=marker,
            label=label,
            color=color,
        )

        # loop through bars and caps and set the alpha value
        with suppress_exception(KeyError):
            [bar.set_alpha(line["error_y"]["opacity"]) for bar in bars]
            [cap.set_alpha(line["error_y"]["opacity"]) for cap in caps]

    with suppress_exception(KeyError):
        if layout["yaxis"]["title"] is not None:
            ax.set_ylabel(layout["yaxis"]["title"])

    with suppress_exception(KeyError):
        if layout["xaxis"]["title"] is not None:
            ax.set_xlabel(layout["xaxis"]["title"])

    with suppress_exception(KeyError):
        ax.set_ylim(layout["yaxis"]["range"])

    with suppress_exception(KeyError):
        ax.set_xlim(layout["xaxis"]["range"])

    with suppress_exception(KeyError):
        ax.xaxis.grid(layout["xaxis"]["showgrid"])

    with suppress_exception(KeyError):
        ax.yaxis.grid(layout["yaxis"]["showgrid"])

    with suppress_exception(KeyError):
        if layout["xaxis"]["tickvals"] is not None:
            ax.xaxis.set_ticks(layout["xaxis"]["tickvals"])

    with suppress_exception(KeyError):
        if layout["xaxis"]["ticktext"] is not None:
            max_len = max([len(txt) for txt in layout["xaxis"]["ticktext"]])
            rot = 45 if max_len > tick_length_before_rotation else 0
            ha = "right" if max_len > tick_length_before_rotation else "center"
            ax.xaxis.set_ticklabels(layout["xaxis"]["ticktext"],
                                    rotation=rot,
                                    ha=ha)

    with suppress_exception(KeyError):
        if layout["xaxis"]["showticklabels"] is not None and not layout[
                "xaxis"]["showticklabels"]:
            ax.set_xticklabels([''] * len(list(ax.get_xticklabels())))

    with suppress_exception(KeyError):
        if layout["yaxis"]["tickvals"] is not None:
            ax.yaxis.set_ticks(layout["yaxis"]["tickvals"])

    with suppress_exception(KeyError):
        if layout["yaxis"]["ticktext"] is not None:
            max_len = max([len(txt) for txt in layout["yaxis"]["ticktext"]])
            rot = 45 if max_len > tick_length_before_rotation else 0
            ha = "right" if max_len > tick_length_before_rotation else "center"
            ax.yaxis.set_ticklabels(layout["yaxis"]["ticktext"],
                                    rotation=rot,
                                    ha=ha)

    with suppress_exception(KeyError):
        if layout["yaxis"]["showticklabels"] is not None and not layout[
                "yaxis"]["showticklabels"]:
            ax.set_yticklabels([''] * len(list(ax.get_yticklabels())))

    if legend_cols > 0:
        leg = ps.make_top_legend(ax, legend_cols)
    # use _set_loc, makes finding the location later on easier
    # leg._set_loc((-.7 if n_legend == 1 else -.88, 1.05))
    # fig.tight_layout()

    return fig
def get_colors():
    """ Insert a new color for line 2 to fit to measurements"""
    if not HACKS:
        return ps.get_mpl_color()
    return (ps.get_mpl_color(i) for i in [0, 3, 1, 2, 4, 5, 6])
コード例 #6
0
def plot_bbq_data(bbq_df,
                  interval=None, xmin=None, xmax=None, ymin=None, ymax=None,
                  output=None, show=True, two_plots=False):
    """ Plot BBQ data.

    Args:
        bbq_df: BBQ Dataframe with moving average columns
        interval: start and end time of used interval, will be marked with red bars
        xmin: Lower x limit (time)
        xmax: Upper x limit (time)
        ymin: Lower y limit (tune)
        ymax: Upper y limit (tune)
        output: Path to the output file
        show: Shows plot if `True`
        two_plots: Plots each tune in it's own axes if `True`

    Returns:
        Plotted figure

    """
    LOG.debug("Plotting BBQ data.")

    ps.set_style("standard", {
        u'figure.figsize': [12.24, 7.68],
        u"lines.marker": u"",
        u"lines.linestyle": u""}
        )

    fig = plt.figure()

    if two_plots:
        gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1])
        ax = [fig.add_subplot(gs[1]), fig.add_subplot(gs[0])]
    else:
        gs = gridspec.GridSpec(1, 1, height_ratios=[1])
        ax = fig.add_subplot(gs[0])
        ax = [ax, ax]

    bbq_df.index = [datetime.datetime.fromtimestamp(time, tz=TIMEZONE) for time in bbq_df.index]

    handles = [None] * (3 * len(PLANES))
    for idx, plane in enumerate(PLANES):
        color = ps.get_mpl_color(idx)
        mask = bbq_df[COL_IN_MAV(plane)]

        # plot and save handles for nicer legend
        handles[idx] = ax[idx].plot(bbq_df.index, bbq_df[COL_BBQ(plane)],
                                    color=ps.change_color_brightness(color, .4),
                                    marker="o", markerfacecolor="None",
                                    label="$Q_{:s}$".format(plane.lower(),)
                                    )[0]
        filtered_data = bbq_df.loc[mask, COL_BBQ(plane)].dropna()
        handles[len(PLANES)+idx] = ax[idx].plot(filtered_data.index, filtered_data.values,
                                                color=ps.change_color_brightness(color, .7),
                                                marker=".",
                                                label="filtered".format(plane.lower())
                                                )[0]
        handles[2*len(PLANES)+idx] = ax[idx].plot(bbq_df.index, bbq_df[COL_MAV(plane)],
                                                  color=color,
                                                  linestyle="-",
                                                  label="moving av.".format(plane.lower())
                                                  )[0]

        if ymin is None and two_plots:
            ax[idx].set_ylim(bottom=min(bbq_df.loc[mask, COL_BBQ(plane)]))

        if ymax is None and two_plots:
            ax[idx].set_ylim(top=max(bbq_df.loc[mask, COL_BBQ(plane)]))

    # things to add/do only once if there is only one plot
    for idx in range(1+two_plots):
        if interval:
            ax[idx].axvline(x=interval[0], color="red")
            ax[idx].axvline(x=interval[1], color="red")

        if two_plots:
            ax[idx].set_ylabel("$Q_{:s}$".format(PLANES[idx]))
        else:
            ax[idx].set_ylabel('Tune')

        ax[idx].set_ylim(bottom=ymin, top=ymax)
        ax[idx].yaxis.set_major_formatter(FormatStrFormatter('%.5f'))

        ax[idx].set_xlim(left=xmin, right=xmax)
        ax[idx].set_xlabel('Time')
        ax[idx].xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))

        if idx:
            # don't show labels on upper plot (if two plots)
            # use the visibility to allow cursor x-position to be shown
            ax[idx].tick_params(labelbottom=False)
            ax[idx].xaxis.get_label().set_visible(False)

        if not two_plots or idx:
            # reorder legend
            ax[idx].legend(handles, [h.get_label() for h in handles],
                           loc='lower right', bbox_to_anchor=(1.0, 1.01), ncol=3,)

    fig.tight_layout()
    fig.tight_layout()

    if output:
        fig.savefig(output)
        ps.set_name(os.path.basename(output))

    if show:
        plt.draw()

    return fig
コード例 #7
0
ファイル: tune_diagram.py プロジェクト: JoschD/bbs_udillyties
def _get_lc(order):
    """ Return line color for order """
    return ps.get_mpl_color(order - 1)