def test_curve_has_all_errorbars_on_replot_after_error_every_increase(self):
     fig = figure()
     ax = fig.add_subplot(111)
     curve = ax.errorbar([0, 1, 2, 4], [0, 1, 2, 4], yerr=[0.1, 0.2, 0.3, 0.4])
     new_curve = CurvesTabWidgetPresenter._replot_mpl_curve(ax, curve,
                                                            {'errorevery': 2})
     self.assertEqual(2, len(new_curve[2][0].get_segments()))
     new_curve = CurvesTabWidgetPresenter._replot_mpl_curve(ax, new_curve,
                                                            {'errorevery': 1})
     self.assertTrue(hasattr(new_curve, 'errorbar_data'))
     self.assertEqual(4, len(new_curve[2][0].get_segments()))
 def _generate_presenter(self, mock_view=None, fig=None):
     if not mock_view:
         mock_view = Mock(get_selected_ax_name=lambda: "Axes 0: (0, 0)",
                          get_selected_curve_name=lambda: "Workspace")
     if not fig:
         fig = self.fig
     return CurvesTabWidgetPresenter(fig=fig, view=mock_view)
Beispiel #3
0
    def __init__(self, fig, view=None, parent=None):
        self.fig = fig
        if view:
            self.view = view
        else:
            self.view = PlotConfigDialogView(parent)
        self.view.show()

        self.tab_widget_presenters = [None, None, None]
        self.tab_widget_views = [None, None, None]
        # Axes tab
        if len(self.fig.get_axes()) > 0:
            axes_tab = AxesTabWidgetPresenter(self.fig, parent=self.view)
            self.tab_widget_presenters[0] = axes_tab
            self.tab_widget_views[0] = (axes_tab.view, "Axes")
        # Curves tab
        if curve_in_figure(self.fig):
            curves_tab = CurvesTabWidgetPresenter(self.fig, parent=self.view)
            self.tab_widget_presenters[1] = curves_tab
            self.tab_widget_views[1] = (curves_tab.view, "Curves")
        # Images tab
        if image_in_figure(self.fig):
            images_tab = ImagesTabWidgetPresenter(self.fig, parent=self.view)
            self.tab_widget_presenters[2] = images_tab
            self.tab_widget_views[2] = (images_tab.view, "Images")

        self._add_tab_widget_views()

        # Signals
        self.view.ok_button.clicked.connect(self.apply_properties_and_exit)
        self.view.apply_button.clicked.connect(self.apply_properties)
        self.view.cancel_button.clicked.connect(self.exit)
Beispiel #4
0
    def toggle_error_bars_for(ax, curve, make_visible=None):
        # get all curve properties
        curve_props = CurveProperties.from_curve(curve)
        # and remove the ones that matplotlib doesn't recognise
        plot_kwargs = curve_props.get_plot_kwargs()
        new_curve = CurvesTabWidgetPresenter.replot_curve(
            ax, curve, plot_kwargs)

        # Inverts either the current state of hide_errors
        # or the make_visible kwarg that forces a state:
        # If make visible is True, then hide_errors must be False
        # for the intended effect
        curve_props.hide_errors = not curve_props.hide_errors if make_visible is None else not make_visible

        CurvesTabWidgetPresenter.toggle_errors(new_curve, curve_props)
        CurvesTabWidgetPresenter.update_limits_and_legend(ax)
    def test_curves_and_legend_tabs_close_when_all_curves_removed(self):
        fig, ax = subplots()
        ax.plot([[0], [0]], label='line1')
        ax.legend()

        mock_view = Mock(get_selected_ax_name=lambda: "(0, 0)",
                         get_current_curve_name=lambda: "line1",
                         get_selected_curves_names=lambda: ["line1"])
        mock_view.select_curve_list=Mock(selectedItems=lambda: [], count=lambda: 0)
        mock_view.select_axes_combo_box.count = Mock(return_value=0)

        parent_presenter_mock = Mock()

        legend_presenter = LegendTabWidgetPresenter(fig=fig, view=mock_view, parent_presenter=parent_presenter_mock)
        curves_presenter =CurvesTabWidgetPresenter(
            fig=fig, view=mock_view, parent_presenter=parent_presenter_mock, legend_tab=legend_presenter)

        curves_presenter.remove_selected_curves()

        curves_presenter.parent_presenter.forget_tab_from_presenter.assert_has_calls(
            [call(curves_presenter), call(legend_presenter)])
        mock_view.close.assert_called()
Beispiel #6
0
    def __init__(self, fig, view=None, parent=None):
        self.fig = fig
        if view:
            self.view = view
        else:
            self.view = PlotConfigDialogView(parent)
        self.view.show()

        self.tab_widget_presenters = [None, None, None, None]
        self.tab_widget_views = [None, None, None, None]
        legend_tab = None
        # Axes tab
        if len(self.fig.get_axes()) > 0:
            axes_tab = AxesTabWidgetPresenter(
                self.fig,
                parent=self.view,
                success_callback=self.success_callback,
                error_callback=self.error_callback)
            self.tab_widget_presenters[1] = axes_tab
            self.tab_widget_views[0] = (axes_tab.view, "Axes")
        # Legend tab
        if legend_in_figure(self.fig):
            legend_tab = LegendTabWidgetPresenter(self.fig,
                                                  parent_view=self.view,
                                                  parent_presenter=self)
            self.tab_widget_presenters[0] = legend_tab
            self.tab_widget_views[3] = (legend_tab.view, "Legend")
        # Curves tab
        if curve_in_figure(self.fig):
            curves_tab = CurvesTabWidgetPresenter(self.fig,
                                                  parent_view=self.view,
                                                  parent_presenter=self,
                                                  legend_tab=legend_tab)
            self.tab_widget_presenters[2] = curves_tab
            self.tab_widget_views[1] = (curves_tab.view, "Curves")
        # Images tab
        if image_in_figure(self.fig):
            images_tab = ImagesTabWidgetPresenter(self.fig, parent=self.view)
            self.tab_widget_presenters[3] = images_tab
            self.tab_widget_views[2] = (images_tab.view, "Images")

        self._add_tab_widget_views()

        # Signals
        self.view.ok_button.clicked.connect(self.apply_properties_and_exit)
        self.view.apply_button.clicked.connect(self.apply_properties)
        self.view.cancel_button.clicked.connect(self.exit)
        self.view.help_button.clicked.connect(self.open_help_window)

        self.error_state = False
Beispiel #7
0
    def add_error_bars_menu(self, parent_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 parent_menu: The menu to which the actions will be added
        :type parent_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, parent_menu)
        error_bars_menu.addAction(
            self.SHOW_ERROR_BARS_BUTTON_TEXT,
            partial(self._update_plot_after,
                    self._toggle_all_errors,
                    ax,
                    make_visible=True))
        error_bars_menu.addAction(
            self.HIDE_ERROR_BARS_BUTTON_TEXT,
            partial(self._update_plot_after,
                    self._toggle_all_errors,
                    ax,
                    make_visible=False))
        parent_menu.addMenu(error_bars_menu)

        self.active_lines = CurvesTabWidgetPresenter.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.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._update_plot_after,
                                self.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._update_plot_after,
                                 self.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)