Skip to content
Snippets Groups Projects
Unverified Commit 4eb6f50f authored by Gagik Vardanyan's avatar Gagik Vardanyan Committed by GitHub
Browse files

Merge pull request #27740 from martyngigg/27736-sliceviewer-mpl-3.1

Fix sliceviewer for matplotlib 3.1
parents ee110df2 b2009e88
No related branches found
No related tags found
No related merge requests found
......@@ -7,15 +7,22 @@
# This file is part of the mantid package
from __future__ import absolute_import
from collections import namedtuple
from contextlib import contextmanager
from matplotlib import cm
from matplotlib import cm, __version__ as mpl_version_str
from matplotlib.container import ErrorbarContainer
from matplotlib.legend import Legend
from enum import Enum
# matplotlib version information
MPLVersionInfo = namedtuple("MPLVersionInfo", ("major", "minor", "patch"))
MATPLOTLIB_VERSION_INFO = MPLVersionInfo._make(map(int, mpl_version_str.split(".")))
# Use the correct draggable method based on the matplotlib version
if hasattr(Legend, "set_draggable"):
SET_DRAGGABLE_METHOD = "set_draggable"
else:
......@@ -132,6 +139,11 @@ def legend_set_draggable(legend, state, use_blit=False, update='loc'):
getattr(legend, SET_DRAGGABLE_METHOD)(state, use_blit, update)
def mpl_version_info():
"""Returns a namedtuple of (major,minor,patch)"""
return MATPLOTLIB_VERSION_INFO
def zoom_axis(ax, coord, x_or_y, factor):
"""
Zoom in around the value 'coord' along the given axis.
......
......@@ -8,22 +8,24 @@
#
#
from __future__ import (absolute_import, division, print_function)
from qtpy.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QComboBox, QCheckBox, QLabel
from qtpy.QtCore import Signal
from qtpy.QtGui import QDoubleValidator
from mantid.plots.utility import mpl_version_info
from mantidqt.MPLwidgets import FigureCanvas
from matplotlib.colorbar import Colorbar
from matplotlib.figure import Figure
from mantidqt.MPLwidgets import FigureCanvas
from matplotlib.colors import Normalize, SymLogNorm, PowerNorm
from matplotlib import cm
import numpy as np
from qtpy.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QComboBox, QCheckBox, QLabel
from qtpy.QtCore import Signal
from qtpy.QtGui import QDoubleValidator
NORM_OPTS = ["Linear", "SymmetricLog10", "Power"]
class ColorbarWidget(QWidget):
colorbarChanged = Signal() # The parent should simply redraw their canvas
colorbarChanged = Signal() # The parent should simply redraw their canvas
def __init__(self, parent=None):
super(ColorbarWidget, self).__init__(parent)
......@@ -108,18 +110,21 @@ class ColorbarWidget(QWidget):
except AttributeError: # else default
cmap = None
self.colorbar = Colorbar(ax=self.ax, mappable=mappable)
self.cmin_value, self.cmax_value = self.colorbar.get_clim()
self.cmin_value, self.cmax_value = mappable.get_clim()
self.update_clim_text()
self.cmap_changed(cmap)
self.cmap.setCurrentIndex(sorted(cm.cmap_d.keys()).index(self.colorbar.get_cmap().name))
self.cmap.setCurrentIndex(sorted(cm.cmap_d.keys()).index(mappable.get_cmap().name))
self.redraw()
def cmap_index_changed(self):
self.cmap_changed(self.cmap.currentText())
def cmap_changed(self, name):
self.colorbar.set_cmap(name)
self.colorbar.mappable.set_cmap(name)
if mpl_version_info() >= (3, 1):
self.colorbar.update_normal(self.colorbar.mappable)
else:
self.colorbar.set_cmap(name)
self.redraw()
def mappable_changed(self):
......@@ -128,13 +133,11 @@ class ColorbarWidget(QWidget):
when the plot changes via settings.
"""
mappable_cmap = self.colorbar.mappable.get_cmap()
low,high = self.colorbar.mappable.get_clim()
self.colorbar.set_cmap(mappable_cmap)
self.colorbar.set_clim(low,high)
low, high = self.colorbar.mappable.get_clim()
self.cmin_value = low
self.cmax_value = high
self.update_clim_text()
self.cmap.setCurrentIndex(sorted(cm.cmap_d.keys()).index(self.colorbar.get_cmap().name))
self.cmap.setCurrentIndex(sorted(cm.cmap_d.keys()).index(mappable_cmap.name))
self.redraw()
def norm_changed(self):
......@@ -208,9 +211,9 @@ class ColorbarWidget(QWidget):
cmax = float(self.cmax.text())
if cmax > self.cmin_value:
self.cmax_value = cmax
else: #reset values back
else: # reset values back
self.update_clim_text()
self.colorbar.set_clim(self.cmin_value, self.cmax_value)
self.colorbar.mappable.set_clim(self.cmin_value, self.cmax_value)
self.redraw()
def update_clim_text(self):
......
......@@ -51,8 +51,6 @@ class SliceViewerNavigationToolbar(NavigationToolbar2QT):
if tooltip_text is not None:
a.setToolTip(tooltip_text)
self.buttons = {}
# Add the x,y location widget at the right side of the toolbar
# The stretch factor is 1 which means any resizing of the toolbar
# will resize this label instead of the buttons.
......
......@@ -8,6 +8,7 @@
#
#
from __future__ import (absolute_import, division, print_function)
from matplotlib import gridspec
from matplotlib.figure import Figure
from matplotlib.transforms import Bbox, BboxTransform
......@@ -149,14 +150,14 @@ class SliceViewerView(QWidget):
self.presenter.line_plots()
def clear_line_plots(self):
try: # clear old plots
try: # clear old plots
del self.xfig
del self.yfig
except AttributeError:
pass
def update_data_clim(self):
self.im.set_clim(self.colorbar.colorbar.get_clim()) # force clim update, needed for RHEL7
self.im.set_clim(self.colorbar.colorbar.mappable.get_clim())
self.canvas.draw_idle()
def update_line_plot_limits(self):
......
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