Пример #1
0
 def _nice_axes(self, ax):
     """ Makes the axes look nicer """
     ax.ticklabel_format(axis='y', style='sci', scilimits=(-2, 3))
     pstyle.set_xaxis_label(ax)
     try:
         pstyle.set_xLimits(self.twiss_df.SEQUENCE, ax)
     except pstyle.ArgumentError:
         pass
     if self._ip_pos is not None and len(self._ip_pos) > 0:
         pstyle.show_ir(self._ip_pos, ax)
Пример #2
0
def _create_single_plot(x_cols, y_cols, e_cols, twiss_data, legends, labels,
                        xy, change_marker, no_legend, auto_scale):
    """ Create plots per parameter """
    # create layout
    if xy:
        plane_map = {0: "X", 1: "Y"}
        gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1])
    else:
        plane_map = None
        gs = gridspec.GridSpec(1, 1, height_ratios=[1])

    ir_pos = None
    x_is_length = all([xc == "S" for xc in x_cols])
    if x_is_length:
        ir_pos = _find_ir_pos(twiss_data)

    # create figure
    fig = plt.figure()
    data = twiss_data[
        0]  # kept it to be more similar with other plotting function

    p_title = labels[0]
    fig.canvas.set_window_title("File '{:s}'".format(p_title))

    for plt_idx in range(1 + xy):
        ax = fig.add_subplot(gs[plt_idx])

        for idx_col, (x_col, y_col, e_col,
                      legend) in enumerate(zip(x_cols, y_cols, e_cols,
                                               legends)):
            LOG.debug("Plotting parameter '{:s}'".format(y_col))

            # plot data
            if xy:
                y_name = y_col
                y_full = y_col + plane_map[plt_idx]
                e_full = e_col + plane_map[plt_idx]

            else:
                y_name = y_col[:-1]
                y_full = y_col
                e_full = e_col

            x_val = data[x_col]
            y_val = data[y_full]
            try:
                e_val = data[e_full]
            except KeyError:
                e_val = None

            _, caps, bars = ax.errorbar(x_val,
                                        y_val,
                                        yerr=e_val,
                                        ls=rcParams[u"lines.linestyle"],
                                        fmt=get_marker(idx_col, change_marker),
                                        label=legend)

            # loop through bars and caps and set the alpha value
            [bar.set_alpha(ERROR_ALPHA) for bar in bars]
            [cap.set_alpha(ERROR_ALPHA) for cap in caps]

            if x_is_length and (idx_col + 1) == len(x_cols):
                try:
                    ps.set_xLimits(data.SEQUENCE, ax)
                except (AttributeError, ps.ArgumentError):
                    pass

            if auto_scale:
                lim = _get_auto_scale(y_val, auto_scale)
                ax.set_ylim(*lim)

            # manage layout
            if len(legends) == 1:
                if legend is None:
                    try:
                        # if it's a recognized column make nice label
                        ps.set_yaxis_label(_map_proper_name(y_name),
                                           y_full[-1], ax)
                    except (KeyError, ps.ArgumentError):
                        ax.set_ylabel(y_full)
                else:
                    # use given legend/label
                    if xy:
                        legend += plane_map[plt_idx]
                    ax.set_ylabel(legend)

        if xy and plt_idx == 0:
            ax.axes.get_xaxis().set_visible(False)
            if x_is_length and ir_pos:
                ps.show_ir(ir_pos, ax, mode='lines')
        else:
            if x_is_length:
                ps.set_xaxis_label(ax)
                if ir_pos:
                    ps.show_ir(ir_pos, ax, mode='outside')

        if not no_legend and plt_idx == 0:
            ax.legend(loc='upper center',
                      bbox_to_anchor=(0.5, 1.25),
                      fancybox=True,
                      shadow=True,
                      ncol=3)
    return fig
Пример #3
0
def _create_plots(x_cols, y_cols, e_cols, twiss_data, legends, labels, xy,
                  change_marker, no_legend, auto_scale):
    """ Create plots per parameter """
    # create layout
    if xy:
        plane_map = {0: "X", 1: "Y"}
        gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1])
    else:
        plane_map = None
        gs = gridspec.GridSpec(1, 1, height_ratios=[1])

    ir_pos = _find_ir_pos(twiss_data)

    # create individual figures
    figs = {}
    for idx_col, (x_col, y_col,
                  e_col) in enumerate(zip(x_cols, y_cols, e_cols)):
        LOG.debug("Plotting parameter '{:s}'".format(y_col))

        # create figure
        fig = plt.figure()

        p_title = y_col
        if xy:
            p_title += " X/Y"
        fig.canvas.set_window_title("Parameter '{:s}'".format(p_title))

        # plot data
        for plt_idx in range(1 + xy):
            if xy:
                y_name = y_col
                y_full = y_col + plane_map[plt_idx]
                e_full = e_col + plane_map[plt_idx]

            else:
                y_name = y_col[:-1]
                y_full = y_col
                e_full = e_col

            ax = fig.add_subplot(gs[plt_idx])

            for idx, data in enumerate(twiss_data):
                x_val = data[x_col]
                y_val = data[y_full]
                try:
                    e_val = data[e_full]
                except KeyError:
                    e_val = None

                _, caps, bars = ax.errorbar(x_val,
                                            y_val,
                                            yerr=e_val,
                                            ls=rcParams[u"lines.linestyle"],
                                            fmt=get_marker(idx, change_marker),
                                            label=legends[idx])

                # loop through bars and caps and set the alpha value
                [bar.set_alpha(ERROR_ALPHA) for bar in bars]
                [cap.set_alpha(ERROR_ALPHA) for cap in caps]

                if x_col == "S" and (idx + 1) == len(twiss_data):
                    try:
                        ps.set_xLimits(data.SEQUENCE, ax)
                    except (AttributeError, ps.ArgumentError):
                        pass

                if auto_scale:
                    current_y_lims = _get_auto_scale(y_val, auto_scale)
                    if idx == 0:
                        y_lims = current_y_lims
                    else:
                        y_lims = [
                            min(y_lims[0], current_y_lims[0]),
                            max(y_lims[1], current_y_lims[1])
                        ]

            # manage layout
            if auto_scale:
                ax.set_ylim(*y_lims)

            if labels[idx_col] is None:
                try:
                    # if it's a recognized column make nice label
                    ps.set_yaxis_label(_map_proper_name(y_name), y_full[-1],
                                       ax)
                except (KeyError, ps.ArgumentError):
                    ax.set_ylabel(y_full)
            else:
                try:
                    # if it's a recognized name make nice label
                    ps.set_yaxis_label(_map_proper_name(labels[idx_col][:-1]),
                                       labels[idx_col][-1], ax)
                except (KeyError, ps.ArgumentError):
                    # use given label
                    ax.set_ylabel(labels[idx_col])

            if xy and plt_idx == 0:
                ax.axes.get_xaxis().set_visible(False)
                if x_col == "S" and ir_pos:
                    ps.show_ir(ir_pos, ax, mode='lines')
            else:
                if x_col == "S":
                    ps.set_xaxis_label(ax)
                    if ir_pos:
                        ps.show_ir(ir_pos, ax, mode='outside')

            if not no_legend and plt_idx == 0:
                ax.legend(loc='upper center',
                          bbox_to_anchor=(0.5, 1.25),
                          fancybox=True,
                          shadow=True,
                          ncol=3)
            figs[y_col] = fig
    return figs
Пример #4
0
def _create_plots(x_cols, y_cols, e_cols, twiss_data, legends, labels, xy,
                  change_marker, no_legend):
    """ Create plots per parameter """
    _param_map = {
        "BET": "beta",
        "BB": "betabeat",
        "D": "dispersion",
        "ND": "norm_dispersion",
        "MU": "phase",
        "X": "co",
        "Y": "co",
    }

    # create layout
    if xy:
        plane_map = {0: "X", 1: "Y"}
        gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1])
    else:
        plane_map = None
        gs = gridspec.GridSpec(1, 1, height_ratios=[1])

    ir_pos = _find_ir_pos(twiss_data)

    # create individual figures
    figs = {}
    for x_col, y_col, e_col in zip(x_cols, y_cols, e_cols):
        LOG.debug("Plotting parameter '{:s}'".format(y_col))

        # create figure
        fig = plt.figure()

        p_title = y_col
        if xy:
            p_title += " X/Y"
        fig.canvas.set_window_title("Parameter '{:s}'".format(p_title))

        # plot data
        for plt_idx in range(1 + xy):
            if xy:
                y_name = y_col
                y_full = y_col + plane_map[plt_idx]
                e_full = e_col + plane_map[plt_idx]

            else:
                y_name = y_col[:-1]
                y_full = y_col
                e_full = e_col

            ax = fig.add_subplot(gs[plt_idx])

            for idx, data in enumerate(twiss_data):
                x_val = data[x_col]
                y_val = data[y_full]
                try:
                    e_val = data[e_full]
                except KeyError:
                    e_val = None

                ax.errorbar(x_val,
                            y_val,
                            yerr=e_val,
                            fmt=get_marker(idx, change_marker),
                            label=legends[idx])

                if (idx + 1) == len(twiss_data):
                    try:
                        ps.set_xLimits(data.SEQUENCE, ax)
                    except (AttributeError, ps.ArgumentError):
                        pass

            # manage layout
            if labels[plt_idx] is None:
                try:
                    # if it's a recognized column make nice label
                    ps.set_yaxis_label(_param_map[y_name], y_full[-1], ax)
                except (KeyError, ps.ArgumentError):
                    ax.set_ylabel(y_full)
            else:
                # use given label
                ax.set_ylabel(labels[plt_idx])

            if xy and plt_idx == 0:
                ax.axes.get_xaxis().set_visible(False)
                if ir_pos:
                    ps.show_ir(ir_pos, ax, mode='lines')
            else:
                ps.set_xaxis_label(ax)
                if ir_pos:
                    ps.show_ir(ir_pos, ax, mode='outside')

            if not no_legend and plt_idx == 0:
                ax.legend(loc='upper center',
                          bbox_to_anchor=(0.5, 1.25),
                          fancybox=True,
                          shadow=True,
                          ncol=3)
            figs[y_col] = fig
    return figs
def plot_analysis(opt):
    """ Plots the specified results

    For required opt-structure look in parse_options.
    """
    LOG.debug("Plotting GetLLM analysis.")
    mdl_analysis = opt.subnode in mdl_subnodes

    ps.set_style("standard", MANUAL_STYLE)
    xmin = min(opt.xplot_xmin, opt.yplot_xmin)
    xmax = max(opt.xplot_xmax, opt.yplot_xmax)

    gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1])
    ax_x = plt.subplot(gs[0])
    ax_y = None
    ir_pos = None

    paths = opt.path.split(',')

    if opt.label == 'None':
        if mdl_analysis:
            labels = [
                "mo_" + opt.path.rsplit('/', 1)[-1],
                "me_" + opt.path.rsplit('/', 1)[-1]
            ]
        else:
            labels = paths
    else:
        labels = opt.label.split(',')

    for idx, path in enumerate(paths):
        data_x, data_y = get_data(path, opt.mainnode, opt.subnode)
        plot_data(ax_x, data_x, labels, idx, opt.change_marker)

        if ir_pos is None:
            ir_pos = get_irpos(data_x, opt.accel)

        if data_y is not None:
            if ax_y is None:
                ax_x.axes.get_xaxis().set_visible(False)
                ax_y = plt.subplot(gs[1])
            plot_data(ax_y, data_y, labels, idx, opt.change_marker)

    ax_x.set_xlim(xmin, xmax)
    ax_x.set_ylim(opt.xplot_ymin, opt.xplot_ymax)
    set_yaxis_label(ax_x, 'x', opt.subnode)

    if ax_y is not None:
        ax_y.set_xlim(xmin, xmax)
        ax_y.set_ylim(opt.yplot_ymin, opt.yplot_ymax)
        set_yaxis_label(ax_y, 'y', opt.subnode)
        ps.set_xaxis_label(ax_y)
        if ir_pos:
            ps.show_ir(ir_pos, ax_y, mode='outside')
            ps.show_ir(ir_pos, ax_x, mode='lines')
    else:
        ax_x.axes.get_xaxis().set_visible(True)
        ps.set_xaxis_label(ax_x)
        if ir_pos:
            ps.show_ir(ir_pos, ax_x, mode='outside')

    if int(opt.legendh) > 12:
        show_legend(ax_x, int(opt.legendx), int(opt.legendy))
    return gs