diff --git a/Framework/PythonInterface/mantid/plots/legend.py b/Framework/PythonInterface/mantid/plots/legend.py index c0b5a7a087cb091472d366ab9b577a4c2a1570f6..088e17b883d30fae6eaf13e55630b9a64c3aeb53 100644 --- a/Framework/PythonInterface/mantid/plots/legend.py +++ b/Framework/PythonInterface/mantid/plots/legend.py @@ -52,6 +52,11 @@ class LegendProperties(dict): else: props['title'] = None + # For Matplotlib <3.2 we have to remove the 'None' string from the title + # which is generated by set_text() in text.py + if props['title'] == 'None': + props['title'] = '' + props['title_font'] = title.get_fontname() props['title_size'] = title.get_fontsize() props['title_color'] = convert_color_to_hex(title.get_color()) @@ -127,10 +132,6 @@ class LegendProperties(dict): @classmethod def create_legend(cls, props, ax): - # For Matplotlib <3.2 we have to remove the 'None' string from the title - # which is generated by set_text() in text.py - if props['title'] == 'None': - props['title'] = '' if int(matplotlib.__version__[0]) >= 2: legend = ax.legend(ncol=props['columns'], prop={'size': props['entries_size']}, diff --git a/qt/applications/workbench/workbench/plotting/test/test_utility.py b/qt/applications/workbench/workbench/plotting/test/test_utility.py index 7667d24f23bafa1314d5d2dc727402d9cb7ba3c1..4191d819d0b567081cac91c4fb23fc42b6499fc1 100644 --- a/qt/applications/workbench/workbench/plotting/test/test_utility.py +++ b/qt/applications/workbench/workbench/plotting/test/test_utility.py @@ -10,6 +10,7 @@ from __future__ import absolute_import import unittest import matplotlib.pyplot as plt +from mantid.plots.legend import LegendProperties from numpy import testing as np_testing from mantid.plots.utility import zoom, zoom_axis @@ -25,12 +26,21 @@ class TestUtility(unittest.TestCase): ax.plot([0, 1, 2], [6, 4, 6]) zoom_point = [2, 6] - factor = 1/1.1 # = 0.909090... + factor = 1 / 1.1 # = 0.909090... xlims, ylims = zoom(ax, *zoom_point, factor=factor) np_testing.assert_almost_equal([-0.31, 2.11], xlims) np_testing.assert_almost_equal([3.69, 6.11], ylims) + def test_from_legend_correctly_returns_properties_if_title_is_none(self): + fig, ax = plt.subplots() + ax.plot([0, 1, 2], [6, 4, 6]) + ax.legend(labels='A', title=None) + + legend_props = LegendProperties.from_legend(ax.legend_) + + self.assertEqual(legend_props['title'], '') + if __name__ == '__main__': unittest.main()