Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Various improvements.
  • Loading branch information
anntzer committed May 29, 2017
commit 56e9f1b6346390c924c1dc5a21b87c62655e9751
54 changes: 37 additions & 17 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from matplotlib._pylab_helpers import Gcf
from matplotlib.figure import Figure

from matplotlib.widgets import SubplotTool
import matplotlib.backends.qt_editor.figureoptions as figureoptions

from .qt_compat import (QtCore, QtGui, QtWidgets, _getSaveFileName,
Expand Down Expand Up @@ -778,7 +777,7 @@ def save_figure(self, *args):
QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.NoButton)


class SubplotToolQt(SubplotTool, UiSubplotTool):
class SubplotToolQt(UiSubplotTool):
def __init__(self, targetfig, parent):
UiSubplotTool.__init__(self, None)

Expand All @@ -790,39 +789,60 @@ def __init__(self, targetfig, parent):
self._widgets[higher].valueChanged.connect(
lambda val: self._widgets[lower].setMaximum(val - .001))

self.defaults = {
attr: getattr(self._figure.subplotpars, attr)
for attr in ["left", "bottom", "right", "top", "wspace", "hspace"]}
self._attrs = ["top", "bottom", "left", "right", "hspace", "wspace"]
self._defaults = {attr: vars(self._figure.subplotpars)[attr]
for attr in self._attrs}

# Set values after setting the range callbacks, but before setting up
# the redraw callbacks.
self._reset()

for attr in self.defaults:
for attr in self._attrs:
self._widgets[attr].valueChanged.connect(self._on_value_changed)
for action, method in [("Tight Layout", self._tight_layout),
for action, method in [("Export values", self._export_values),
("Tight layout", self._tight_layout),
("Reset", self._reset),
("Close", self.close)]:
self._widgets[action].clicked.connect(method)

def _export_values(self):
# Explicitly round to 3 decimals (which is also the spinbox precision)
# to avoid numbers of the form 0.100...001.
dialog = QtWidgets.QDialog()
layout = QtWidgets.QVBoxLayout()
dialog.setLayout(layout)
text = QtWidgets.QPlainTextEdit()
text.setReadOnly(True)
layout.addWidget(text)
text.setPlainText(
",\n".join("{}={:.3}".format(attr, self._widgets[attr].value())
for attr in self._attrs))
# Adjust the height of the text widget to fit the whole text, plus
# some padding.
size = text.maximumSize()
size.setHeight(
QtGui.QFontMetrics(text.document().defaultFont())
.size(0, text.toPlainText()).height() + 20)
text.setMaximumSize(size)
dialog.exec_()

def _on_value_changed(self):
self._figure.subplots_adjust(
**{attr: self._widgets[attr].value() for attr in self.defaults})
if self.drawon:
self._figure.canvas.draw_idle()
self._figure.subplots_adjust(**{attr: self._widgets[attr].value()
for attr in self._attrs})
self._figure.canvas.draw_idle()

def _tight_layout(self):
self._figure.tight_layout()
for attr in self.defaults:
for attr in self._attrs:
widget = self._widgets[attr]
widget.blockSignals(True)
widget.setValue(getattr(self._figure.subplotpars, attr))
widget.setValue(vars(self._figure.subplotpars)[attr])
widget.blockSignals(False)
if self.drawon:
self._figure.canvas.draw_idle()
self._figure.canvas.draw_idle()

def _reset(self):
for attr in self.defaults:
self._widgets[attr].setValue(self.defaults[attr])
for attr, value in self._defaults.items():
self._widgets[attr].setValue(value)


def error_msg_qt(msg, parent=None):
Expand Down
10 changes: 8 additions & 2 deletions lib/matplotlib/backends/qt_editor/formsubplottool.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ def __init__(self, *args, **kwargs):
widget.setSingleStep(.005)
widget.setKeyboardTracking(False)
inner.addRow(side, widget)
right.addStretch(1)

for action in ["Tight Layout", "Reset", "Close"]:
widget = QtWidgets.QPushButton("Export values")
self._widgets["Export values"] = widget
# Don't trigger on <enter>, which is used to input values.
widget.setAutoDefault(False)
left.addWidget(widget)

for action in ["Tight layout", "Reset", "Close"]:
self._widgets[action] = widget = QtWidgets.QPushButton(action)
# Don't trigger on <enter>, which is used to input values.
widget.setAutoDefault(False)
right.addWidget(widget)

Expand Down