Skip to content
Snippets Groups Projects
Commit ebf56b60 authored by Martyn Gigg's avatar Martyn Gigg
Browse files

Add helper function to set legend draggable status

The API changed in matplotlib 3.1 but we still need to support
matplotlib > 1.5.
parent 98a3b82d
No related branches found
No related tags found
No related merge requests found
...@@ -11,10 +11,17 @@ from contextlib import contextmanager ...@@ -11,10 +11,17 @@ from contextlib import contextmanager
from matplotlib import cm from matplotlib import cm
from matplotlib.container import ErrorbarContainer from matplotlib.container import ErrorbarContainer
from matplotlib.legend import Legend
from enum import Enum 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 # Any changes here must be reflected in the definition in
# the C++ MplCpp/Plot.h header. See the comment in that file # the C++ MplCpp/Plot.h header. See the comment in that file
# for the reason for duplication. # for the reason for duplication.
...@@ -117,6 +124,14 @@ def get_autoscale_limits(ax, axis): ...@@ -117,6 +124,14 @@ def get_autoscale_limits(ax, axis):
return locator.view_limits(axis_min, axis_max) 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): def zoom_axis(ax, coord, x_or_y, factor):
""" """
Zoom in around the value 'coord' along the given axis. Zoom in around the value 'coord' along the given axis.
......
...@@ -3,11 +3,9 @@ add_subdirectory(modest_image) ...@@ -3,11 +3,9 @@ add_subdirectory(modest_image)
# mantid.dataobjects tests # mantid.dataobjects tests
set(TEST_PY_FILES set(TEST_PY_FILES
helperfunctionsTest.py helperfunctionsTest.py plotfunctionsTest.py plotfunctions3DTest.py
plotfunctionsTest.py plots__init__Test.py ScalesTest.py UtilityTest.py
plotfunctions3DTest.py )
plots__init__Test.py
ScalesTest.py)
check_tests_valid(${CMAKE_CURRENT_SOURCE_DIR} ${TEST_PY_FILES}) check_tests_valid(${CMAKE_CURRENT_SOURCE_DIR} ${TEST_PY_FILES})
......
# 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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment