def _test_toggle_normalization(self, errorbars_on, plot_kwargs): fig = plot([self.ws], spectrum_nums=[1], errors=errorbars_on, plot_kwargs=plot_kwargs) mock_canvas = MagicMock(figure=fig) fig_manager_mock = MagicMock(canvas=mock_canvas) fig_interactor = FigureInteraction(fig_manager_mock) # Earlier versions of matplotlib do not store the data assciated with a # line with high precision and hence we need to set a lower tolerance # when making comparisons of this data if matplotlib.__version__ < "2": decimal_tol = 1 else: decimal_tol = 7 ax = fig.axes[0] fig_interactor._toggle_normalization(ax) assert_almost_equal(ax.lines[0].get_xdata(), [15, 25]) assert_almost_equal(ax.lines[0].get_ydata(), [0.2, 0.3], decimal=decimal_tol) self.assertEqual("Counts ($\\AA$)$^{-1}$", ax.get_ylabel()) fig_interactor._toggle_normalization(ax) assert_almost_equal(ax.lines[0].get_xdata(), [15, 25]) assert_almost_equal(ax.lines[0].get_ydata(), [2, 3], decimal=decimal_tol) self.assertEqual("Counts", ax.get_ylabel())
def test_correct_yunit_label_when_overplotting_after_normaliztion_toggle( self): # The earlier version of Matplotlib on RHEL throws an error when performing the second # plot in this test, if the lines have errorbars. The error occurred when it attempted # to draw an interactive legend. Plotting without errors still fulfills the purpose of this # test, so turn them off for old Matplotlib versions. errors = True if int(matplotlib.__version__[0]) < 2: errors = False fig = plot([self.ws], spectrum_nums=[1], errors=errors, plot_kwargs={'distribution': True}) mock_canvas = MagicMock(figure=fig) fig_manager_mock = MagicMock(canvas=mock_canvas) fig_interactor = FigureInteraction(fig_manager_mock) ax = fig.axes[0] fig_interactor._toggle_normalization(ax) self.assertEqual("Counts ($\AA$)$^{-1}$", ax.get_ylabel()) plot([self.ws1], spectrum_nums=[1], errors=errors, overplot=True, fig=fig) self.assertEqual("Counts ($\AA$)$^{-1}$", ax.get_ylabel())
def test_log_maintained_when_normalisation_toggled(self): ws = CreateWorkspace(DataX=[1, 2, 3, 4, 2, 4, 6, 8], DataY=[2] * 8, NSpec=2, OutputWorkspace="ragged_ws") fig = pcolormesh_from_names([ws]) mock_canvas = MagicMock(figure=fig) fig_manager_mock = MagicMock(canvas=mock_canvas) fig_interactor = FigureInteraction(fig_manager_mock) fig_interactor._change_colorbar_axes(LogNorm) fig_interactor._toggle_normalization(fig.axes[0]) self.assertTrue(isinstance(fig.axes[0].images[-1].norm, LogNorm))
def test_scale_on_ragged_workspaces_maintained_when_toggling_normalisation(self): ws = CreateWorkspace(DataX=[1, 2, 3, 4, 2, 4, 6, 8], DataY=[2] * 8, NSpec=2, OutputWorkspace="ragged_ws") fig = pcolormesh_from_names([ws]) mock_canvas = MagicMock(figure=fig) fig_manager_mock = MagicMock(canvas=mock_canvas) fig_interactor = FigureInteraction(fig_manager_mock) fig_interactor._toggle_normalization(fig.axes[0]) clim = fig.axes[0].images[0].get_clim() fig_interactor._toggle_normalization(fig.axes[0]) self.assertEqual(clim, fig.axes[0].images[0].get_clim()) self.assertNotEqual((-0.1, 0.1), fig.axes[0].images[0].get_clim())
def test_toggle_normalisation_on_contour_plot_maintains_contour_line_colour(self): from mantid.plots.legend import convert_color_to_hex ws = CreateWorkspace(DataX=[1, 2, 3, 4, 2, 4, 6, 8], DataY=[2] * 8, NSpec=2, OutputWorkspace="test_ws") fig = plot_contour([ws]) for col in fig.get_axes()[0].collections: col.set_color("#ff9900") mock_canvas = MagicMock(figure=fig) fig_manager_mock = MagicMock(canvas=mock_canvas) fig_interactor = FigureInteraction(fig_manager_mock) fig_interactor._toggle_normalization(fig.axes[0]) self.assertTrue(all(convert_color_to_hex(col.get_color()[0]) == "#ff9900" for col in fig.get_axes()[0].collections))
def test_toggle_normalisation_applies_to_all_images_if_one_colorbar(self): fig = pcolormesh([self.ws, self.ws]) mock_canvas = MagicMock(figure=fig) fig_manager_mock = MagicMock(canvas=mock_canvas) fig_interactor = FigureInteraction(fig_manager_mock) # there should be 3 axes, 2 colorplots and 1 colorbar self.assertEqual(3, len(fig.axes)) fig.axes[0].tracked_workspaces.values() self.assertTrue(fig.axes[0].tracked_workspaces['ws'][0].is_normalized) self.assertTrue(fig.axes[1].tracked_workspaces['ws'][0].is_normalized) fig_interactor._toggle_normalization(fig.axes[0]) self.assertFalse(fig.axes[0].tracked_workspaces['ws'][0].is_normalized) self.assertFalse(fig.axes[1].tracked_workspaces['ws'][0].is_normalized)
def test_correct_yunit_label_when_overplotting_after_normaliztion_toggle( self): fig = plot([self.ws], spectrum_nums=[1], errors=True, plot_kwargs={'distribution': True}) mock_canvas = MagicMock(figure=fig) fig_manager_mock = MagicMock(canvas=mock_canvas) fig_interactor = FigureInteraction(fig_manager_mock) ax = fig.axes[0] fig_interactor._toggle_normalization(ax) self.assertEqual("Counts ($\AA$)$^{-1}$", ax.get_ylabel()) plot([self.ws1], spectrum_nums=[1], errors=True, overplot=True, fig=fig) self.assertEqual("Counts ($\AA$)$^{-1}$", ax.get_ylabel())