Beispiel #1
0
def plot_md_histo_ws(workspaces,
                     errors=False,
                     overplot=False,
                     fig=None,
                     ax_properties=None,
                     window_title=None):
    """

    :param workspaces:
    :param errors:
    :param overplot:
    :param fig:
    :return:
    """
    # MDHistoWorkspace
    # Get figure and Axes
    num_axes = 1
    fig, axes = get_plot_fig(overplot, ax_properties, window_title, num_axes,
                             fig)
    axes = [
        MantidAxes.from_mpl_axes(ax, ignore_artists=[Legend])
        if not isinstance(ax, MantidAxes) else ax for ax in axes
    ]

    # Plot MD
    _do_single_plot_mdhisto_workspace(axes[0], workspaces, errors)

    return _update_show_figure(fig)
Beispiel #2
0
    def deleteHandle(self, _, workspace):
        """
        Called when the ADS has deleted a workspace. Checks the
        attached axes for any hold a plot from this workspace. If removing
        this leaves empty axes then the parent window is triggered for
        closer
        :param _: The name of the workspace. Unused
        :param workspace: A pointer to the workspace
        """
        # Find the axes with this workspace reference
        all_axes = self.canvas.figure.axes
        if not all_axes:
            return

        # Here we wish to delete any curves linked to the workspace being
        # deleted and if a figure is now empty, close it. We must avoid closing
        # any figures that were created via the script window that are not
        # managed via a workspace.
        # See https://github.com/mantidproject/mantid/issues/25135.
        empty_axes = []
        redraw = False
        for ax in all_axes:
            if isinstance(ax, MantidAxes):
                to_redraw = ax.remove_workspace_artists(workspace)
            else:
                to_redraw = False
            # Solution for filtering out colorbar axes. Works most of the time.
            if type(ax) is not Axes:
                empty_axes.append(MantidAxes.is_empty(ax))
            redraw = redraw | to_redraw

        if all(empty_axes):
            self.window.emit_close()
        elif redraw:
            self.canvas.draw()
Beispiel #3
0
def plot(workspaces,
         spectrum_nums=None,
         wksp_indices=None,
         errors=False,
         overplot=False,
         fig=None,
         plot_kwargs=None,
         ax_properties=None,
         window_title=None):
    """
    Create a figure with a single subplot and for each workspace/index add a
    line plot to the new axes. show() is called before returning the figure instance. A legend
    is added.

    :param workspaces: A list of workspace handles
    :param spectrum_nums: A list of spectrum number identifiers (general start from 1)
    :param wksp_indices: A list of workspace indexes (starts from 0)
    :param errors: If true then error bars are added for each plot
    :param overplot: If true then overplot over the current figure if one exists
    :param fig: If not None then use this Figure object to plot
    :param plot_kwargs: Arguments that will be passed onto the plot function
    :param ax_properties: A dict of axes properties. E.g. {'yscale': 'log'}
    :param window_title: A string denoting name of the GUI window which holds the graph
    :return: The figure containing the plots
    """
    if plot_kwargs is None:
        plot_kwargs = {}
    _validate_plot_inputs(workspaces, spectrum_nums, wksp_indices)
    if spectrum_nums is not None:
        kw, nums = 'specNum', spectrum_nums
    else:
        kw, nums = 'wkspIndex', wksp_indices

    if fig is None:
        # get/create the axes to hold the plot
        fig, ax = get_plot_fig(overplot, ax_properties, window_title)
    else:
        ax = fig.gca()

    if not isinstance(ax, MantidAxes):
        # Convert to a MantidAxes if it isn't already. Ignore legend since
        # a new one will be drawn later
        ax = MantidAxes.from_mpl_axes(ax, ignore_artists=[Legend])

    # do the plotting
    plot_fn = ax.errorbar if errors else ax.plot
    for ws in workspaces:
        for num in nums:
            plot_kwargs[kw] = num
            plot_fn(ws, **plot_kwargs)

    ax.legend().draggable()
    if not overplot:
        title = workspaces[0].name()
        ax.set_title(title)
        fig.canvas.set_window_title(figure_title(workspaces, fig.number))
    fig.canvas.draw()
    fig.show()
    return fig
Beispiel #4
0
 def test_from_mpl_axes_success_with_default_args(self):
     plt.figure()
     plt.plot([0, 1], [0, 1])
     plt.plot([0, 2], [0, 2])
     ax = plt.gca()
     mantid_ax = MantidAxes.from_mpl_axes(ax)
     self.assertEqual(len(mantid_ax.lines), 2)
     self.assertIsInstance(mantid_ax, MantidAxes)
Beispiel #5
0
 def test_from_mpl_axes_success_with_default_args(self):
     plt.figure()
     plt.plot([0, 1], [0, 1])
     plt.plot([0, 2], [0, 2])
     ax = plt.gca()
     mantid_ax = MantidAxes.from_mpl_axes(ax)
     self.assertEqual(len(mantid_ax.lines), 2)
     self.assertIsInstance(mantid_ax, MantidAxes)
Beispiel #6
0
def plot(workspaces, spectrum_nums=None, wksp_indices=None, errors=False,
         overplot=False, fig=None, plot_kwargs=None, ax_properties=None,
         window_title=None):
    """
    Create a figure with a single subplot and for each workspace/index add a
    line plot to the new axes. show() is called before returning the figure instance. A legend
    is added.

    :param workspaces: A list of workspace handles
    :param spectrum_nums: A list of spectrum number identifiers (general start from 1)
    :param wksp_indices: A list of workspace indexes (starts from 0)
    :param errors: If true then error bars are added for each plot
    :param overplot: If true then overplot over the current figure if one exists
    :param fig: If not None then use this Figure object to plot
    :param plot_kwargs: Arguments that will be passed onto the plot function
    :param ax_properties: A dict of axes properties. E.g. {'yscale': 'log'}
    :param window_title: A string denoting name of the GUI window which holds the graph
    :return: The figure containing the plots
    """
    if plot_kwargs is None:
        plot_kwargs = {}
    _validate_plot_inputs(workspaces, spectrum_nums, wksp_indices)
    if spectrum_nums is not None:
        kw, nums = 'specNum', spectrum_nums
    else:
        kw, nums = 'wkspIndex', wksp_indices

    if fig is None:
        # get/create the axes to hold the plot
        fig, ax = get_plot_fig(overplot, ax_properties, window_title)
    else:
        ax = fig.gca()

    if not isinstance(ax, MantidAxes):
        # Convert to a MantidAxes if it isn't already. Ignore legend since
        # a new one will be drawn later
        ax = MantidAxes.from_mpl_axes(ax, ignore_artists=[Legend])

    # do the plotting
    plot_fn = ax.errorbar if errors else ax.plot
    for ws in workspaces:
        for num in nums:
            plot_kwargs[kw] = num
            plot_fn(ws, **plot_kwargs)

    ax.legend().draggable()
    if not overplot:
        title = workspaces[0].name()
        ax.set_title(title)
        fig.canvas.set_window_title(figure_title(workspaces, fig.number))
    fig.canvas.draw()
    fig.show()
    return fig
Beispiel #7
0
    def deleteHandle(self, _, workspace):
        """
        Called when the ADS has deleted a workspace. Checks the
        attached axes for any hold a plot from this workspace. If removing
        this leaves empty axes then the parent window is triggered for
        closer
        :param _: The name of the workspace. Unused
        :param workspace: A pointer to the workspace
        """
        # Find the axes with this workspace reference
        all_axes = self.canvas.figure.axes
        if not all_axes:
            return

        # Here we wish to delete any curves linked to the workspace being
        # deleted and if a figure is now empty, close it. We must avoid closing
        # any figures that were created via the script window that are not
        # managed via a workspace.
        # See https://github.com/mantidproject/mantid/issues/25135.
        empty_axes = []
        redraw = False
        for ax in all_axes:
            if isinstance(ax, MantidAxes):
                to_redraw = ax.remove_workspace_artists(workspace)
            else:
                to_redraw = False
            # We check for axes type below as a pseudo check for an axes being
            # a colorbar. Creating a colorfill plot creates 2 axes: one linked
            # to a workspace, the other a colorbar. Deleting the workspace
            # deletes the colorfill, but the plot remains open due to the
            # non-empty colorbar. This solution seems to work for the majority
            # of cases but could lead to unmanaged figures only containing an
            # Axes object being closed.
            if type(ax) is not Axes:
                empty_axes.append(MantidAxes.is_empty(ax))
            redraw = redraw | to_redraw

        if all(empty_axes):
            self.window.emit_close()
        elif redraw:
            self.canvas.draw()
Beispiel #8
0
    def deleteHandle(self, _, workspace):
        """
        Called when the ADS has deleted a workspace. Checks the
        attached axes for any hold a plot from this workspace. If removing
        this leaves empty axes then the parent window is triggered for
        closer
        :param _: The name of the workspace. Unused
        :param workspace: A pointer to the workspace
        """
        # Find the axes with this workspace reference
        all_axes = self.canvas.figure.axes
        if not all_axes:
            return

        # Here we wish to delete any curves linked to the workspace being
        # deleted and if a figure is now empty, close it. We must avoid closing
        # any figures that were created via the script window that are not
        # managed via a workspace.
        # See https://github.com/mantidproject/mantid/issues/25135.
        empty_axes = []
        for ax in all_axes:
            if isinstance(ax, MantidAxes):
                ax.remove_workspace_artists(workspace)
            # We check for axes type below as a pseudo check for an axes being
            # a colorbar. Creating a colorfill plot creates 2 axes: one linked
            # to a workspace, the other a colorbar. Deleting the workspace
            # deletes the colorfill, but the plot remains open due to the
            # non-empty colorbar. This solution seems to work for the majority
            # of cases but could lead to unmanaged figures only containing an
            # Axes object being closed.
            if type(ax) is not Axes:
                empty_axes.append(MantidAxes.is_empty(ax))

        if all(empty_axes):
            self.window.emit_close()
        else:
            self.canvas.draw_idle()
Beispiel #9
0
def plot(workspaces,
         spectrum_nums=None,
         wksp_indices=None,
         errors=False,
         overplot=False,
         fig=None,
         plot_kwargs=None,
         ax_properties=None,
         window_title=None,
         tiled=False,
         waterfall=False,
         log_name=None,
         log_values=None):
    """
    Create a figure with a single subplot and for each workspace/index add a
    line plot to the new axes. show() is called before returning the figure instance. A legend
    is added.

    :param workspaces: A list of workspace handles or strings
    :param spectrum_nums: A list of spectrum number identifiers (general start from 1)
    :param wksp_indices: A list of workspace indexes (starts from 0)
    :param errors: If true then error bars are added for each plot
    :param overplot: If true then overplot over the current figure if one exists. If an axis object the overplotting
    will be done on the axis passed in
    :param fig: If not None then use this Figure object to plot
    :param plot_kwargs: Arguments that will be passed onto the plot function
    :param ax_properties: A dict of axes properties. E.g. {'yscale': 'log'}
    :param window_title: A string denoting name of the GUI window which holds the graph
    :param tiled: An optional flag controlling whether to do a tiled or overlayed plot
    :param waterfall: An optional flag controlling whether or not to do a waterfall plot
    :param log_name: The optional log being plotted against.
    :param log_values: An optional list of log values to plot against.
    :return: The figure containing the plots
    """
    plot_font = ConfigService.getString('plots.font')
    if plot_font:
        if len(mpl.rcParams['font.family']) > 1:
            mpl.rcParams['font.family'][0] = plot_font
        else:
            mpl.rcParams['font.family'].insert(0, plot_font)

    if plot_kwargs is None:
        plot_kwargs = {}
    _validate_plot_inputs(workspaces, spectrum_nums, wksp_indices, tiled,
                          overplot)
    workspaces = [ws for ws in workspaces if isinstance(ws, MatrixWorkspace)]

    if spectrum_nums is not None:
        kw, nums = 'specNum', spectrum_nums
    else:
        kw, nums = 'wkspIndex', wksp_indices

    _add_default_plot_kwargs_from_settings(plot_kwargs, errors)

    num_axes = len(workspaces) * len(nums) if tiled else 1

    fig, axes = get_plot_fig(overplot, ax_properties, window_title, num_axes,
                             fig)

    # Convert to a MantidAxes if it isn't already. Ignore legend since
    # a new one will be drawn later
    axes = [
        MantidAxes.from_mpl_axes(ax, ignore_artists=[Legend])
        if not isinstance(ax, MantidAxes) else ax for ax in axes
    ]

    assert axes, "No axes are associated with this plot"

    if tiled:
        ws_index = [(ws, index) for ws in workspaces for index in nums]
        for index, ax in enumerate(axes):
            if index < len(ws_index):
                _do_single_plot(ax, [ws_index[index][0]], errors, False,
                                [ws_index[index][1]], kw, plot_kwargs)
            else:
                ax.axis('off')
    else:
        show_title = ("on" == ConfigService.getString(
            "plots.ShowTitle").lower()) and not overplot
        ax = overplot if isinstance(overplot, MantidAxes) else axes[0]
        ax.axis('on')
        _do_single_plot(ax, workspaces, errors, show_title, nums, kw,
                        plot_kwargs, log_name, log_values)

    # Can't have a waterfall plot with only one line.
    if len(nums) * len(workspaces) == 1 and waterfall:
        waterfall = False

    # The plot's initial xlim and ylim are used to offset each curve in a waterfall plot.
    # Need to do this whether the current curve is a waterfall plot or not because it may be converted later.
    if not overplot:
        datafunctions.set_initial_dimensions(ax)

    if waterfall:
        ax.set_waterfall(True)

    if not overplot:
        fig.canvas.set_window_title(figure_title(workspaces, fig.number))
    else:
        if ax.is_waterfall():
            for i in range(len(nums) * len(workspaces)):
                errorbar_cap_lines = datafunctions.remove_and_return_errorbar_cap_lines(
                    ax)
                datafunctions.convert_single_line_to_waterfall(
                    ax,
                    len(ax.get_lines()) - (i + 1))

                if ax.waterfall_has_fill():
                    datafunctions.waterfall_update_fill(ax)

                ax.lines += errorbar_cap_lines

    # update and show figure
    return _update_show_figure(fig)
Beispiel #10
0
def plot(workspaces,
         spectrum_nums=None,
         wksp_indices=None,
         errors=False,
         overplot=False,
         fig=None,
         plot_kwargs=None,
         ax_properties=None,
         window_title=None,
         tiled=False):
    """
    Create a figure with a single subplot and for each workspace/index add a
    line plot to the new axes. show() is called before returning the figure instance. A legend
    is added.

    :param workspaces: A list of workspace handles or strings
    :param spectrum_nums: A list of spectrum number identifiers (general start from 1)
    :param wksp_indices: A list of workspace indexes (starts from 0)
    :param errors: If true then error bars are added for each plot
    :param overplot: If true then overplot over the current figure if one exists. If an axis object the overplotting
    will be done on the axis passed in
    :param fig: If not None then use this Figure object to plot
    :param plot_kwargs: Arguments that will be passed onto the plot function
    :param ax_properties: A dict of axes properties. E.g. {'yscale': 'log'}
    :param window_title: A string denoting name of the GUI window which holds the graph
    :param tiled: An optional flag controlling whether to do a tiled or overlayed plot
    :return: The figure containing the plots
    """
    if plot_kwargs is None:
        plot_kwargs = {}
    _validate_plot_inputs(workspaces, spectrum_nums, wksp_indices, tiled,
                          overplot)

    if spectrum_nums is not None:
        kw, nums = 'specNum', spectrum_nums
    else:
        kw, nums = 'wkspIndex', wksp_indices

    num_axes = len(workspaces) * len(nums) if tiled else 1

    fig, axes = get_plot_fig(overplot, ax_properties, window_title, num_axes,
                             fig)

    # Convert to a MantidAxes if it isn't already. Ignore legend since
    # a new one will be drawn later
    axes = [
        MantidAxes.from_mpl_axes(ax, ignore_artists=[Legend])
        if not isinstance(ax, MantidAxes) else ax for ax in axes
    ]

    if tiled:
        ws_index = [(ws, index) for ws in workspaces for index in nums]
        for index, ax in enumerate(axes):
            if index < len(ws_index):
                _do_single_plot(ax, [ws_index[index][0]], errors, False,
                                [ws_index[index][1]], kw, plot_kwargs)
            else:
                ax.axis('off')
    else:
        ax = overplot if isinstance(overplot, MantidAxes) else axes[0]
        ax.axis('on')
        _do_single_plot(ax, workspaces, errors, not overplot, nums, kw,
                        plot_kwargs)

    if not overplot:
        fig.canvas.set_window_title(figure_title(workspaces, fig.number))
    # This updates the toolbar so the home button now takes you back to this point, the try catch is in case the manager does not
    # have a toolbar attatched.
    try:
        fig.canvas.manager.toolbar.update()
    except AttributeError:
        pass
    fig.canvas.draw()
    fig.show()
    return fig