def show(self): self.window.show() self.window.activateWindow() self.window.raise_() if self.window.windowState() & Qt.WindowMinimized: # windowState() stores a combination of window state enums # and multiple window states can be valid. On Windows # a window can be both minimized and maximized at the # same time, so we make a check here. For more info see: # http://doc.qt.io/qt-5/qt.html#WindowState-enum if self.window.windowState() & Qt.WindowMaximized: self.window.setWindowState(Qt.WindowMaximized) else: self.window.setWindowState(Qt.WindowNoState) # Hack to ensure the canvas is up to date self.canvas.draw_idle() if figure_type(self.canvas.figure) not in [FigureType.Line, FigureType.Errorbar] \ or self.toolbar is not None and len(self.canvas.figure.get_axes()) > 1: self._set_fit_enabled(False) # For plot-to-script button to show, we must have a MantidAxes with lines in it # Plot-to-script currently doesn't work with waterfall plots so the button is hidden for that plot type. if not any((isinstance(ax, MantidAxes) and curve_in_ax(ax)) for ax in self.canvas.figure.get_axes() ) or self.canvas.figure.get_axes()[0].is_waterfall(): self.toolbar.set_generate_plot_script_enabled(False) # Only show options specific to waterfall plots if the axes is a MantidAxes and is a waterfall plot. if not isinstance(self.canvas.figure.get_axes()[0], MantidAxes) or not self.canvas.figure.get_axes( )[0].is_waterfall(): self.toolbar.set_waterfall_options_enabled(False)
def set_buttons_visibility(self, fig): if figure_type(fig) not in [FigureType.Line, FigureType.Errorbar ] or len(fig.get_axes()) > 1: self.set_fit_enabled(False) self.set_superplot_enabled(False) # if any of the lines are a sample log plot disable fitting for ax in fig.get_axes(): for artist in ax.get_lines(): try: if ax.get_artists_sample_log_plot_details( artist) is not None: self.set_fit_enabled(False) break except Exception: # The artist is not tracked - ignore this one and check the rest continue # For plot-to-script button to show, every axis must be a MantidAxes with lines in it # Plot-to-script currently doesn't work with waterfall plots so the button is hidden for that plot type. if not all((isinstance(ax, MantidAxes) and curve_in_ax(ax)) for ax in fig.get_axes()) or \ fig.get_axes()[0].is_waterfall(): self.set_generate_plot_script_enabled(False) # reenable script generation for colormaps if self.is_colormap(fig): self.set_generate_plot_script_enabled(True) # Only show options specific to waterfall plots if the axes is a MantidAxes and is a waterfall plot. if not isinstance(fig.get_axes()[0], MantidAxes) or not fig.get_axes()[0].is_waterfall(): self.set_waterfall_options_enabled(False) # For contour and wireframe plots, add a toolbar option to change the colour of the lines. if figure_type(fig) in [FigureType.Wireframe, FigureType.Contour]: self.set_up_color_selector_toolbar_button(fig) if figure_type(fig) in [ FigureType.Surface, FigureType.Wireframe, FigureType.Mesh ]: self.adjust_for_3d_plots() # Determine the toggle state of the grid button. If all subplots have grids, then set it to on. is_major_grid_on = False for ax in fig.get_axes(): # Don't look for grids on colour bars. if self._is_colorbar(ax): continue if hasattr(ax, 'grid_on'): is_major_grid_on = ax.grid_on() else: is_major_grid_on = ax.xaxis._major_tick_kw.get( 'gridOn', False) and ax.yaxis._major_tick_kw.get( 'gridOn', False) # If ANY of the axes have no grid, set the button to unchecked. if not is_major_grid_on: break self._actions['toggle_grid'].setChecked(is_major_grid_on)
def populate_select_axes_combo_box(self): """ Add Axes names to 'select axes' combo box. Names are generated similary to in AxesTabWidgetPresenter """ # Sort names by axes position names = [] for name, ax in self.axes_names_dict.items(): if curve_in_ax(ax): names.append(name) names = sorted(names, key=lambda x: x[x.rfind("("):]) self.view.populate_select_axes_combo_box(names)
def generate_script(fig, exclude_headers=False): """ Generate a script to recreate a figure. This currently only supports recreating artists that were plotted from a Workspace. The format of the outputted script is as follows: <Default Workbench script contents (imports)> <Workspace retrieval from ADS> fig, axes = plt.subplots() axes.plot() or axes.errorbar() axes.set_title() axes.set_xlabel and axes.set_ylabel() axes.set_xlim() and axes.set_ylim() axes.set_xscale() and axes.set_yscale() axes.legend().set_draggable(True) (if legend present, or just draggable() for earlier matplotlib versions) plt.show() :param fig: A matplotlib.pyplot.Figure object you want to create a script from :param exclude_headers: Boolean. Set to True to ignore imports/headers :return: A String. A script to recreate the given figure """ plot_commands = [] for ax in fig.get_axes(): if not isinstance(ax, MantidAxes) or not curve_in_ax(ax): continue ax_object_var = get_axes_object_variable(ax) plot_commands.extend(get_plot_cmds(ax, ax_object_var)) # ax.plot plot_commands.extend(get_title_cmds(ax, ax_object_var)) # ax.set_title plot_commands.extend(get_axis_label_cmds( ax, ax_object_var)) # ax.set_label plot_commands.extend(get_axis_limit_cmds(ax, ax_object_var)) # ax.set_lim plot_commands.extend(get_axis_scale_cmds( ax, ax_object_var)) # ax.set_scale plot_commands.extend(get_legend_cmds(ax, ax_object_var)) # ax.legend plot_commands.append('') if not plot_commands: return cmds = [] if exclude_headers else [DEFAULT_SCRIPT_CONTENT] cmds.extend(generate_workspace_retrieval_commands(fig) + ['']) cmds.append("{}, {} = {}".format(FIG_VARIABLE, AXES_VARIABLE, generate_subplots_command(fig))) cmds.extend(plot_commands) cmds.append("fig.show()") cmds.append( "# Scripting Plots in Mantid: https://docs.mantidproject.org/nightly/plotting/scripting_plots.html" ) return '\n'.join(cmds)
def set_buttons_visiblity(self, fig): if figure_type(fig) not in [FigureType.Line, FigureType.Errorbar ] or len(fig.get_axes()) > 1: self.set_fit_enabled(False) # if any of the lines are a sample log plot disable fitting for ax in fig.get_axes(): for artist in ax.get_lines(): try: if ax.get_artists_sample_log_plot_details( artist) is not None: self.set_fit_enabled(False) break except Exception: # The artist is not tracked - ignore this one and check the rest continue # For plot-to-script button to show, every axis must be a MantidAxes with lines in it # Plot-to-script currently doesn't work with waterfall plots so the button is hidden for that plot type. if not all((isinstance(ax, MantidAxes) and curve_in_ax(ax)) for ax in fig.get_axes()) or \ fig.get_axes()[0].is_waterfall(): self.set_generate_plot_script_enabled(False) # reenable script generation for colormaps if self.is_colormap(fig): self.set_generate_plot_script_enabled(True) # Only show options specific to waterfall plots if the axes is a MantidAxes and is a waterfall plot. if not isinstance(fig.get_axes()[0], MantidAxes) or not fig.get_axes()[0].is_waterfall(): self.set_waterfall_options_enabled(False) # For contour and wireframe plots, add a toolbar option to change the colour of the lines. if figure_type(fig) in [FigureType.Wireframe, FigureType.Contour]: self.set_up_color_selector_toolbar_button(fig) if figure_type(fig) in [FigureType.Surface, FigureType.Wireframe]: self.adjust_for_3d_plots()
def show(self): self.window.show() self.window.activateWindow() self.window.raise_() if self.window.windowState() & Qt.WindowMinimized: # windowState() stores a combination of window state enums # and multiple window states can be valid. On Windows # a window can be both minimized and maximized at the # same time, so we make a check here. For more info see: # http://doc.qt.io/qt-5/qt.html#WindowState-enum if self.window.windowState() & Qt.WindowMaximized: self.window.setWindowState(Qt.WindowMaximized) else: self.window.setWindowState(Qt.WindowNoState) # Hack to ensure the canvas is up to date self.canvas.draw_idle() if figure_type(self.canvas.figure) not in [FigureType.Line, FigureType.Errorbar]: self._set_fit_enabled(False) # For plot-to-script button to show, we must have a MantidAxes with lines in it if not any((isinstance(ax, MantidAxes) and curve_in_ax(ax)) for ax in self.canvas.figure.get_axes()): self._set_generate_plot_script_enabled(False)
def generate_script(fig, exclude_headers=False): """ Generate a script to recreate a figure. This currently only supports recreating artists that were plotted from a Workspace. The format of the outputted script is as follows: <Default Workbench script contents (imports)> <Workspace retrieval from ADS> fig, axes = plt.subplots() axes.plot() or axes.errorbar() axes.set_title() axes.set_xlabel and axes.set_ylabel() axes.set_xlim() and axes.set_ylim() axes.set_xscale() and axes.set_yscale() axes.legend().set_draggable(True) (if legend present, or just draggable() for earlier matplotlib versions) plt.show() :param fig: A matplotlib.pyplot.Figure object you want to create a script from :param exclude_headers: Boolean. Set to True to ignore imports/headers :return: A String. A script to recreate the given figure """ plot_commands = [] plot_headers = ['import matplotlib.pyplot as plt'] for ax in fig.get_axes(): if not isinstance(ax, MantidAxes): continue ax_object_var = get_axes_object_variable(ax) if axes_type(ax) in [FigureType.Image]: colormap_lines, colormap_headers = get_plot_2d_cmd(ax, ax_object_var) # ax.imshow or pcolormesh plot_commands.extend(colormap_lines) plot_headers.extend(colormap_headers) else: if not curve_in_ax(ax): continue plot_commands.extend(get_plot_cmds(ax, ax_object_var)) # ax.plot plot_commands.extend(generate_tick_commands(ax)) plot_commands.extend(generate_grid_commands(ax)) plot_commands.extend(get_title_cmds(ax, ax_object_var)) # ax.set_title plot_commands.extend(get_axis_label_cmds(ax, ax_object_var)) # ax.set_label plot_commands.extend(get_axis_limit_cmds(ax, ax_object_var)) # ax.set_lim plot_commands.extend(get_axis_scale_cmds(ax, ax_object_var)) # ax.set_scale plot_commands.extend(get_legend_cmds(ax, ax_object_var)) # ax.legend plot_commands.append('') if not plot_commands: return fit_commands, fit_headers = get_fit_cmds(fig) cmds = [] if exclude_headers else [DEFAULT_SCRIPT_CONTENT] if exclude_headers and fit_headers: cmds.extend(fit_headers) if plot_headers: cmds.extend(plot_headers) if fit_commands: cmds.append(FIT_DOCUMENTATION_STRING) cmds.extend(fit_commands + ['']) cmds.extend(generate_workspace_retrieval_commands(fig) + ['']) cmds.append("{}, {} = {}".format(FIG_VARIABLE, AXES_VARIABLE, generate_subplots_command(fig))) cmds.extend(plot_commands) cmds.append("plt.show()") cmds.append("# Scripting Plots in Mantid:") cmds.append("# https://docs.mantidproject.org/tutorials/python_in_mantid/plotting/02_scripting_plots.html") return '\n'.join(cmds)