def add_error_bars_menu(self, menu, ax): """ Add menu actions to toggle the errors for all lines in the plot. Lines without errors are added in the context menu first, then lines containing errors are appended afterwards. This is done so that the context menu always has the same order of curves as the legend is currently showing - and the legend always appends curves with errors after the lines without errors. Relevant source, as of 10 July 2019: https://github.com/matplotlib/matplotlib/blob/154922992722db37a9d9c8680682ccc4acf37f8c/lib/matplotlib/legend.py#L1201 :param menu: The menu to which the actions will be added :type menu: QMenu :param ax: The Axes containing lines to toggle errors on """ # if the ax is not a MantidAxes, and there are no errors plotted, # then do not add any options for the menu if not isinstance(ax, MantidAxes) and len(ax.containers) == 0: return error_bars_menu = QMenu(self.ERROR_BARS_MENU_TEXT, menu) error_bars_menu.addAction(self.SHOW_ERROR_BARS_BUTTON_TEXT, partial(self.errors_manager.update_plot_after, self.errors_manager.toggle_all_errors, ax, make_visible=True)) error_bars_menu.addAction(self.HIDE_ERROR_BARS_BUTTON_TEXT, partial(self.errors_manager.update_plot_after, self.errors_manager.toggle_all_errors, ax, make_visible=False)) menu.addMenu(error_bars_menu) self.errors_manager.active_lines = self.errors_manager.get_curves_from_ax(ax) # if there's more than one line plotted, then # add a sub menu, containing an action to hide the # error bar for each line error_bars_menu.addSeparator() add_later = [] for index, line in enumerate(self.errors_manager.active_lines): if curve_has_errors(line): curve_props = CurveProperties.from_curve(line) # Add lines without errors first, lines with errors are appended later. Read docstring for more info if not isinstance(line, ErrorbarContainer): action = error_bars_menu.addAction(line.get_label(), partial( self.errors_manager.update_plot_after, self.errors_manager.toggle_error_bars_for, ax, line)) action.setCheckable(True) action.setChecked(not curve_props.hide_errors) else: add_later.append((line.get_label(), partial( self.errors_manager.update_plot_after, self.errors_manager.toggle_error_bars_for, ax, line), not curve_props.hide_errors)) for label, function, visible in add_later: action = error_bars_menu.addAction(label, function) action.setCheckable(True) action.setChecked(visible)
def replot_curve(cls, ax, curve, plot_kwargs): if isinstance(ax, MantidAxes): if ax.creation_args: axis = ax.creation_args[0].get('axis', None) if axis: plot_kwargs['axis'] = axis try: new_curve = ax.replot_artist(curve, errorbars=curve_has_errors(curve), **plot_kwargs) except ValueError: # ValueError raised if Artist not tracked by Axes new_curve = cls._replot_mpl_curve(ax, curve, plot_kwargs) else: new_curve = cls._replot_mpl_curve(ax, curve, plot_kwargs) if hasattr(new_curve, 'errorevery'): setattr(new_curve, 'errorevery', plot_kwargs.get('errorevery', 1)) return new_curve
def set_errorbars_tab_enabled(self): """Enable/disable the errorbar tab for selected curve""" enable_errorbars = curve_has_errors(self.get_selected_curve()) self.view.set_errorbars_tab_enabled(enable_errorbars)
def toggle_all_errors(cls, ax, make_visible): active_lines = cls.get_curves_from_ax(ax) for line in active_lines: if curve_has_errors(line): cls.toggle_error_bars_for(ax, line, make_visible)
def toggle_errors(curve, view_props): if curve_has_errors(curve): hide_errors = view_props.hide_errors or view_props.hide setattr(curve, 'hide_errors', hide_errors) set_errorbars_hidden(curve, hide_errors)
def _toggle_all_errors(self, ax, make_visible): for line in self.active_lines: if curve_has_errors(line): self.toggle_error_bars_for(ax, line, make_visible)