diff --git a/qt/applications/workbench/workbench/plotting/figureinteraction.py b/qt/applications/workbench/workbench/plotting/figureinteraction.py index 1b59e0eec1c590516bbf371f48b22ae9a7ce6351..b8e937504ff36d5588c306d6b7241d1f2dcd02e8 100644 --- a/qt/applications/workbench/workbench/plotting/figureinteraction.py +++ b/qt/applications/workbench/workbench/plotting/figureinteraction.py @@ -299,11 +299,6 @@ class FigureInteraction(object): self._add_normalization_option_menu(menu, event.inaxes) self.add_error_bars_menu(menu, event.inaxes) self._add_marker_option_menu(menu, event) - - # Able to change the plot type to waterfall if there is only one axes, it is a MantidAxes, and there is more - # than one line on the axes. - if len(event.inaxes.get_figure().get_axes()) == 1 and isinstance(event.inaxes, MantidAxes)\ - and len(event.inaxes.get_lines()) > 1: self._add_plot_type_option_menu(menu, event.inaxes) menu.exec_(QCursor.pos()) @@ -441,6 +436,18 @@ class FigureInteraction(object): menu.addMenu(marker_menu) def _add_plot_type_option_menu(self, menu, ax): + # Error bar caps are considered lines so they are removed before checking the number of lines on the axes so + # they aren't confused for "actual" lines. + error_bar_caps = datafunctions.remove_and_return_errorbar_cap_lines(ax) + + # Able to change the plot type to waterfall if there is only one axes, it is a MantidAxes, and there is more + # than one line on the axes. + if len(ax.get_figure().get_axes()) > 1 or not isinstance(ax, MantidAxes) or len(ax.get_lines()) <= 1: + return + + # Re-add error bar caps + ax.lines += error_bar_caps + plot_type_menu = QMenu("Plot Type", menu) plot_type_action_group = QActionGroup(plot_type_menu) standard = plot_type_menu.addAction("1D", lambda: self._change_plot_type( diff --git a/qt/applications/workbench/workbench/plotting/test/test_figureinteraction.py b/qt/applications/workbench/workbench/plotting/test/test_figureinteraction.py index f6c6604b0906b1298cf89d0ea5d4a435d01721c9..20ba7bdc92fa2569c208d1f0a1f0329e44592122 100644 --- a/qt/applications/workbench/workbench/plotting/test/test_figureinteraction.py +++ b/qt/applications/workbench/workbench/plotting/test/test_figureinteraction.py @@ -296,6 +296,35 @@ class FigureInteractionTest(unittest.TestCase): self.assertTrue( any(FigureInteraction.HIDE_ERROR_BARS_BUTTON_TEXT == child.text() for child in added_menu.children())) + def test_context_menu_includes_plot_type_if_plot_has_multiple_lines(self): + fig, self.ax = plt.subplots(subplot_kw={'projection': 'mantid'}) + self.ax.plot([0, 1], [0, 1]) + self.ax.plot([0, 1], [0, 1]) + + main_menu = QMenu() + # QMenu always seems to have 1 child when empty, + # but just making sure the count as expected at this point in the test + self.assertEqual(1, len(main_menu.children())) + + self.interactor._add_plot_type_option_menu(main_menu, self.ax) + + added_menu = main_menu.children()[1] + self.assertEqual(added_menu.children()[0].text(), "Plot Type") + + def test_context_menu_does_not_include_plot_type_if_plot_has_one_line(self): + fig, self.ax = plt.subplots(subplot_kw={'projection': 'mantid'}) + self.ax.errorbar([0, 1], [0, 1], capsize=1) + + main_menu = QMenu() + # QMenu always seems to have 1 child when empty, + # but just making sure the count as expected at this point in the test + self.assertEqual(1, len(main_menu.children())) + + self.interactor._add_plot_type_option_menu(main_menu, self.ax) + + # Number of children should remain unchanged + self.assertEqual(1, len(main_menu.children())) + def test_scripted_plot_show_and_hide_all(self): self.ax.plot([0, 15000], [0, 15000], label='MyLabel') self.ax.errorbar([0, 15000], [0, 14000], yerr=[10, 10000], label='MyLabel 2')