Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8964f7

Browse files
committed
Various improvements.
1 parent 244a3e0 commit a8964f7

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from matplotlib._pylab_helpers import Gcf
2222
from matplotlib.figure import Figure
2323

24-
from matplotlib.widgets import SubplotTool
2524
import matplotlib.backends.qt_editor.figureoptions as figureoptions
2625

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

780779

781-
class SubplotToolQt(SubplotTool, UiSubplotTool):
780+
class SubplotToolQt(UiSubplotTool):
782781
def __init__(self, targetfig, parent):
783782
UiSubplotTool.__init__(self, None)
784783

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

793-
self.defaults = {
794-
attr: getattr(self._figure.subplotpars, attr)
795-
for attr in ["left", "bottom", "right", "top", "wspace", "hspace"]}
792+
self._attrs = ["top", "bottom", "left", "right", "hspace", "wspace"]
793+
self._defaults = {attr: vars(self._figure.subplotpars)[attr]
794+
for attr in self._attrs}
795+
796796
# Set values after setting the range callbacks, but before setting up
797797
# the redraw callbacks.
798798
self._reset()
799799

800-
for attr in self.defaults:
800+
for attr in self._defaults:
801801
self._widgets[attr].valueChanged.connect(self._on_value_changed)
802-
for action, method in [("Tight Layout", self._tight_layout),
802+
for action, method in [("Export values", self._export_values),
803+
("Tight layout", self._tight_layout),
803804
("Reset", self._reset),
804805
("Close", self.close)]:
805806
self._widgets[action].clicked.connect(method)
806807

808+
def _export_values(self):
809+
# Explicitly round to 3 decimals (which is also the spinbox precision)
810+
# to avoid numbers of the form 0.100...001.
811+
dialog = QtWidgets.QDialog()
812+
layout = QtWidgets.QVBoxLayout()
813+
dialog.setLayout(layout)
814+
text = QtWidgets.QPlainTextEdit()
815+
text.setReadOnly(True)
816+
layout.addWidget(text)
817+
text.setPlainText(
818+
",\n".join("{}={:.3}".format(attr, self._widgets[attr].value())
819+
for attr in self._attrs))
820+
# Adjust the height of the text widget to fit the whole text, plus
821+
# some padding.
822+
size = text.maximumSize()
823+
size.setHeight(
824+
QtGui.QFontMetrics(text.document().defaultFont())
825+
.size(0, text.toPlainText()).height() + 20)
826+
text.setMaximumSize(size)
827+
dialog.exec_()
828+
807829
def _on_value_changed(self):
808-
self._figure.subplots_adjust(
809-
**{attr: self._widgets[attr].value() for attr in self.defaults})
810-
if self.drawon:
811-
self._figure.canvas.draw_idle()
830+
self._figure.subplots_adjust(**{attr: self._widgets[attr].value()
831+
for attr in self._attrs})
832+
self._figure.canvas.draw_idle()
812833

813834
def _tight_layout(self):
814835
self._figure.tight_layout()
815-
for attr in self.defaults:
836+
for attr in self._attrs:
816837
widget = self._widgets[attr]
817838
widget.blockSignals(True)
818-
widget.setValue(getattr(self._figure.subplotpars, attr))
839+
widget.setValue(vars(self._figure.subplotpars)[attr])
819840
widget.blockSignals(False)
820-
if self.drawon:
821-
self._figure.canvas.draw_idle()
841+
self._figure.canvas.draw_idle()
822842

823843
def _reset(self):
824-
for attr in self.defaults:
825-
self._widgets[attr].setValue(self.defaults[attr])
844+
for attr in self._defaults:
845+
self._widgets[attr].setValue(self._defaults[attr])
826846

827847

828848
def error_msg_qt(msg, parent=None):

lib/matplotlib/backends/qt_editor/formsubplottool.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ def __init__(self, *args, **kwargs):
4040
widget.setSingleStep(.005)
4141
widget.setKeyboardTracking(False)
4242
inner.addRow(side, widget)
43+
right.addStretch(1)
4344

44-
for action in ["Tight Layout", "Reset", "Close"]:
45+
self._widgets["Export values"] = widget = \
46+
QtWidgets.QPushButton("Export values")
47+
# Don't trigger on <enter>, which is used to input values.
48+
widget.setAutoDefault(False)
49+
left.addWidget(widget)
50+
51+
for action in ["Tight layout", "Reset", "Close"]:
4552
self._widgets[action] = widget = QtWidgets.QPushButton(action)
46-
# Don't trigger on <enter>, which is used to input values.
4753
widget.setAutoDefault(False)
4854
right.addWidget(widget)
4955

0 commit comments

Comments
 (0)