diff --git a/Framework/PythonInterface/mantid/plots/utility.py b/Framework/PythonInterface/mantid/plots/utility.py index 4e02c746b12961434f6a1d815bab46bf307c7321..2e69e98908026787bb9fc91965b4fe7410910aed 100644 --- a/Framework/PythonInterface/mantid/plots/utility.py +++ b/Framework/PythonInterface/mantid/plots/utility.py @@ -11,10 +11,17 @@ from contextlib import contextmanager from matplotlib import cm from matplotlib.container import ErrorbarContainer +from matplotlib.legend import Legend from enum import Enum +if hasattr(Legend, "set_draggable"): + SET_DRAGGABLE_METHOD = "set_draggable" +else: + SET_DRAGGABLE_METHOD = "draggable" + + # Any changes here must be reflected in the definition in # the C++ MplCpp/Plot.h header. See the comment in that file # for the reason for duplication. @@ -117,6 +124,14 @@ def get_autoscale_limits(ax, axis): return locator.view_limits(axis_min, axis_max) +def legend_set_draggable(legend, state, use_blit=False, update='loc'): + """Utility function to support varying Legend api around draggable status across + the versions of matplotlib we support. Function arguments match those from matplotlib. + See matplotlib documentation for argument descriptions + """ + getattr(legend, SET_DRAGGABLE_METHOD)(state, use_blit, update) + + def zoom_axis(ax, coord, x_or_y, factor): """ Zoom in around the value 'coord' along the given axis. diff --git a/Framework/PythonInterface/test/python/mantid/plots/CMakeLists.txt b/Framework/PythonInterface/test/python/mantid/plots/CMakeLists.txt index dfc7e590e48fab793a5be6a9b9fd9423b9ebd128..e62034a5b8d1aa84d48c8f19bc094de220ebbef2 100644 --- a/Framework/PythonInterface/test/python/mantid/plots/CMakeLists.txt +++ b/Framework/PythonInterface/test/python/mantid/plots/CMakeLists.txt @@ -3,11 +3,9 @@ add_subdirectory(modest_image) # mantid.dataobjects tests set(TEST_PY_FILES - helperfunctionsTest.py - plotfunctionsTest.py - plotfunctions3DTest.py - plots__init__Test.py - ScalesTest.py) + helperfunctionsTest.py plotfunctionsTest.py plotfunctions3DTest.py + plots__init__Test.py ScalesTest.py UtilityTest.py +) check_tests_valid(${CMAKE_CURRENT_SOURCE_DIR} ${TEST_PY_FILES}) diff --git a/Framework/PythonInterface/test/python/mantid/plots/UtilityTest.py b/Framework/PythonInterface/test/python/mantid/plots/UtilityTest.py new file mode 100644 index 0000000000000000000000000000000000000000..94d4fd2a3550c3444f8c8e18712c67c786876f0a --- /dev/null +++ b/Framework/PythonInterface/test/python/mantid/plots/UtilityTest.py @@ -0,0 +1,37 @@ +# Mantid Repository : https://github.com/mantidproject/mantid +# +# Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI, +# NScD Oak Ridge National Laboratory, European Spallation Source +# & Institut Laue - Langevin +# SPDX - License - Identifier: GPL - 3.0 + +# This file is part of the mantid package +# Mantid Repository : https://github.com/mantidproject/mantid +# +# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI, +# NScD Oak Ridge National Laboratory, European Spallation Source +# & Institut Laue - Langevin +# SPDX - License - Identifier: GPL - 3.0 + +from __future__ import absolute_import, division + +import unittest + +from mantid.plots.utility import legend_set_draggable +from mantid.py3compat.mock import create_autospec +from matplotlib.legend import Legend + + +class UtilityTest(unittest.TestCase): + + def test_legend_set_draggable(self): + legend = create_autospec(Legend) + args = (None, False, 'loc') + legend_set_draggable(legend, *args) + + if hasattr(Legend, 'set_draggable'): + legend.set_draggable.assert_called_with(*args) + else: + legend.draggable.assert_called_with(*args) + + +if __name__ == '__main__': + unittest.main()