Ejemplo n.º 1
0
 def _setup_figure_widget(self):
     fig, _, _, _ = create_subplots(1)
     fig.axes[0].autoscale(enable=True, tight=False)
     self.figure = fig
     self.figure.canvas = FigureCanvas(self.figure)
     toolbar = MantidNavigationToolbar(self.figure.canvas, self)
     self.figure_layout = QVBoxLayout()
     self.figure_layout.addWidget(toolbar)
     self.figure_layout.addWidget(self.figure.canvas)
     self.layout.addLayout(self.figure_layout)
Ejemplo n.º 2
0
def pcolormesh(workspaces, fig=None):
    """
    Create a figure containing pcolor subplots

    :param workspaces: A list of workspace handles
    :param fig: An optional figure to contain the new plots. Its current contents will be cleared
    :returns: The figure containing the plots
    """
    # check inputs
    _validate_pcolormesh_inputs(workspaces)
    workspaces = [ws for ws in workspaces if isinstance(ws, MatrixWorkspace)]

    # create a subplot of the appropriate number of dimensions
    # extend in number of columns if the number of plottables is not a square number
    workspaces_len = len(workspaces)
    fig, axes, nrows, ncols = create_subplots(workspaces_len, fig=fig)

    row_idx, col_idx = 0, 0
    for subplot_idx in range(nrows * ncols):
        ax = axes[row_idx][col_idx]
        if subplot_idx < workspaces_len:
            ws = workspaces[subplot_idx]
            pcm = pcolormesh_on_axis(ax, ws)
            if pcm:  # Colour bar limits are wrong if workspace is ragged. Set them manually.
                colorbar_min = np.nanmin(pcm.get_array())
                colorbar_max = np.nanmax(pcm.get_array())
                pcm.set_clim(colorbar_min, colorbar_max)
            if col_idx < ncols - 1:
                col_idx += 1
            else:
                row_idx += 1
                col_idx = 0
        else:
            # nothing here
            ax.axis('off')

    # Adjust locations to ensure the plots don't overlap
    fig.subplots_adjust(wspace=SUBPLOT_WSPACE, hspace=SUBPLOT_HSPACE)
    fig.colorbar(pcm, ax=axes.ravel().tolist(), pad=0.06)
    fig.canvas.set_window_title(figure_title(workspaces, fig.number))
    #assert a minimum size, otherwise we can lose axis labels
    size = fig.get_size_inches()
    if (size[0] <= COLORPLOT_MIN_WIDTH) or (size[1] <= COLORPLOT_MIN_HEIGHT):
        fig.set_size_inches(COLORPLOT_MIN_WIDTH, COLORPLOT_MIN_HEIGHT, forward=True)
    fig.canvas.draw()
    fig.show()
    return fig
Ejemplo n.º 3
0
    def make_fig(self, plot_dict, create_plot=True):
        """
        This method currently only considers single matplotlib.axes.Axes based figures as that is the most common case
        :param plot_dict: dictionary; A dictionary of various items intended to recreate a figure
        :param create_plot: Bool; whether or not to make the plot, or to return the figure.
        :return: matplotlib.figure; Only returns if create_plot=False
        """        # Grab creation arguments
        creation_args = plot_dict["creationArguments"]

        if len(creation_args) == 0:
            logger.information(
                "A plot could not be loaded from the save file, as it did not have creation_args. "
                "The original plot title was: {}".format(plot_dict["label"]))
            return

        for sublist in creation_args:
            for cargs_dict in sublist:
                if 'norm' in cargs_dict and type(cargs_dict['norm']) is dict:
                    cargs_dict['norm'] = self.restore_normalise_obj_from_dict(
                        cargs_dict['norm'])
        fig, axes_matrix, _, _ = create_subplots(len(creation_args))
        axes_list = axes_matrix.flatten().tolist()
        for ax, cargs_list in zip(axes_list, creation_args):
            creation_args_copy = copy.deepcopy(cargs_list)
            for cargs in cargs_list:
                if "workspaces" in cargs:
                    workspace_name = cargs.pop("workspaces")
                    workspace = ADS.retrieve(workspace_name)
                    self.workspace_plot_func(workspace, ax, ax.figure, cargs)
                elif "function" in cargs:
                    self.plot_func(ax, cargs)
            for cargs in creation_args_copy:
                cargs.pop('normalize_by_bin_width', None)
            ax.creation_args = creation_args_copy

        # Update the fig
        fig._label = plot_dict["label"]
        if fig.canvas.manager is not None:
            fig.canvas.manager.set_window_title(plot_dict["label"])
        self.restore_figure_data(fig=fig, dic=plot_dict)

        # If the function should create plot then create else return
        if create_plot:
            fig.show()
        else:
            return fig
Ejemplo n.º 4
0
def pcolormesh(workspaces,
               fig=None,
               color_norm=None,
               normalize_by_bin_width=None):
    """
    Create a figure containing pcolor subplots

    :param workspaces: A list of workspace handles
    :param fig: An optional figure to contain the new plots. Its current contents will be cleared
    :param normalize_by_bin_width: Optional and only to be used in the event that the function is being called as part
    of a plot restore
    :returns: The figure containing the plots
    """
    # check inputs
    _validate_pcolormesh_inputs(workspaces)
    workspaces = [ws for ws in workspaces if isinstance(ws, MatrixWorkspace)]

    # create a subplot of the appropriate number of dimensions
    # extend in number of columns if the number of plottables is not a square number
    workspaces_len = len(workspaces)
    fig, axes, nrows, ncols = create_subplots(workspaces_len, fig=fig)

    plots = []
    row_idx, col_idx = 0, 0
    for subplot_idx in range(nrows * ncols):
        ax = axes[row_idx][col_idx]
        if subplot_idx < workspaces_len:
            ws = workspaces[subplot_idx]
            pcm = pcolormesh_on_axis(ax, ws, color_norm,
                                     normalize_by_bin_width)
            plots.append(pcm)
            if col_idx < ncols - 1:
                col_idx += 1
            else:
                row_idx += 1
                col_idx = 0

            if ConfigService.getString("plots.ShowMinorTicks").lower() == "on":
                ax.minorticks_on()

            ax.show_minor_gridlines = ConfigService.getString(
                "plots.ShowMinorGridlines").lower() == "on"
        else:
            # nothing here
            ax.axis('off')

    # If there are multiple plots limits are the min and max of all the plots
    colorbar_min = min(pt.norm.vmin for pt in plots)
    colorbar_max = max(pt.norm.vmax for pt in plots)
    for pt in plots:
        pt.set_clim(colorbar_min, colorbar_max)

    # Adjust locations to ensure the plots don't overlap
    fig.subplots_adjust(wspace=SUBPLOT_WSPACE, hspace=SUBPLOT_HSPACE)

    axes = axes.ravel()
    colorbar = fig.colorbar(pcm, ax=axes.tolist(), pad=0.06)
    add_colorbar_label(colorbar, axes)

    if fig.canvas.manager is not None:
        fig.canvas.manager.set_window_title(
            figure_title(workspaces, fig.number))
    # assert a minimum size, otherwise we can lose axis labels
    size = fig.get_size_inches()
    if (size[0] <= COLORPLOT_MIN_WIDTH) or (size[1] <= COLORPLOT_MIN_HEIGHT):
        fig.set_size_inches(COLORPLOT_MIN_WIDTH,
                            COLORPLOT_MIN_HEIGHT,
                            forward=True)
    fig.canvas.draw()
    fig.show()
    return fig