Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8ecc571

Browse filesBrowse files
authored
Merge pull request #19677 from soininen/qt_figureoptions_axis_limits_conversion
Convert axis limit units in Qt plot options widget
2 parents f044270 + e347200 commit 8ecc571
Copy full SHA for 8ecc571

File tree

Expand file treeCollapse file tree

3 files changed

+56
-9
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+56
-9
lines changed

‎lib/matplotlib/backends/qt_editor/_formlayout.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/qt_editor/_formlayout.py
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,17 @@ def get(self):
342342
elif isinstance(value, Real):
343343
value = float(str(field.text()))
344344
elif isinstance(value, datetime.datetime):
345-
value = field.dateTime().toPyDateTime()
345+
datetime_ = field.dateTime()
346+
if hasattr(datetime_, "toPyDateTime"):
347+
value = datetime_.toPyDateTime()
348+
else:
349+
value = datetime_.toPython()
346350
elif isinstance(value, datetime.date):
347-
value = field.date().toPyDate()
351+
date_ = field.date()
352+
if hasattr(date_, "toPyDate"):
353+
value = date_.toPyDate()
354+
else:
355+
value = date_.toPython()
348356
else:
349357
value = eval(str(field.text()))
350358
valuelist.append(value)

‎lib/matplotlib/backends/qt_editor/figureoptions.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/qt_editor/figureoptions.py
+12-6Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from matplotlib import cbook, cm, colors as mcolors, markers, image as mimage
1111
from matplotlib.backends.qt_compat import QtGui
1212
from matplotlib.backends.qt_editor import _formlayout
13-
13+
from matplotlib.dates import DateConverter, num2date
1414

1515
LINESTYLES = {'-': 'Solid',
1616
'--': 'Dashed',
@@ -33,9 +33,17 @@ def figure_edit(axes, parent=None):
3333
sep = (None, None) # separator
3434

3535
# Get / General
36-
# Cast to builtin floats as they have nicer reprs.
37-
xmin, xmax = map(float, axes.get_xlim())
38-
ymin, ymax = map(float, axes.get_ylim())
36+
def convert_limits(lim, converter):
37+
"""Convert axis limits for correct input editors."""
38+
if isinstance(converter, DateConverter):
39+
return map(num2date, lim)
40+
# Cast to builtin floats as they have nicer reprs.
41+
return map(float, lim)
42+
43+
xconverter = axes.xaxis.converter
44+
xmin, xmax = convert_limits(axes.get_xlim(), xconverter)
45+
yconverter = axes.yaxis.converter
46+
ymin, ymax = convert_limits(axes.get_ylim(), yconverter)
3947
general = [('Title', axes.get_title()),
4048
sep,
4149
(None, "<b>X-Axis</b>"),
@@ -54,8 +62,6 @@ def figure_edit(axes, parent=None):
5462
]
5563

5664
# Save the unit data
57-
xconverter = axes.xaxis.converter
58-
yconverter = axes.yaxis.converter
5965
xunits = axes.xaxis.get_units()
6066
yunits = axes.yaxis.get_units()
6167

‎lib/matplotlib/tests/test_backend_qt.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_backend_qt.py
+34-1Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
from datetime import date, datetime
23
import signal
34
from unittest import mock
45

@@ -10,7 +11,8 @@
1011

1112

1213
try:
13-
from matplotlib.backends.qt_compat import QtGui
14+
from matplotlib.backends.qt_compat import QtGui, QtWidgets
15+
from matplotlib.backends.qt_editor import _formlayout
1416
except ImportError:
1517
pytestmark = pytest.mark.skip('No usable Qt5 bindings')
1618

@@ -245,6 +247,20 @@ def test_figureoptions():
245247
fig.canvas.manager.toolbar.edit_parameters()
246248

247249

250+
@pytest.mark.backend('Qt5Agg', skip_on_importerror=True)
251+
def test_figureoptions_with_datetime_axes():
252+
fig, ax = plt.subplots()
253+
xydata = [
254+
datetime(year=2021, month=1, day=1),
255+
datetime(year=2021, month=2, day=1)
256+
]
257+
ax.plot(xydata, xydata)
258+
with mock.patch(
259+
"matplotlib.backends.qt_editor._formlayout.FormDialog.exec_",
260+
lambda self: None):
261+
fig.canvas.manager.toolbar.edit_parameters()
262+
263+
248264
@pytest.mark.backend('Qt5Agg', skip_on_importerror=True)
249265
def test_double_resize():
250266
# Check that resizing a figure twice keeps the same window size
@@ -282,3 +298,20 @@ def crashing_callback(fig, stale):
282298
canvas = FigureCanvasQTAgg(fig)
283299
fig.stale = True
284300
assert called
301+
302+
303+
@pytest.mark.backend('Qt5Agg', skip_on_importerror=True)
304+
def test_form_widget_get_with_datetime_and_date_fields():
305+
if not QtWidgets.QApplication.instance():
306+
QtWidgets.QApplication()
307+
form = [
308+
("Datetime field", datetime(year=2021, month=3, day=11)),
309+
("Date field", date(year=2021, month=3, day=11))
310+
]
311+
widget = _formlayout.FormWidget(form)
312+
widget.setup()
313+
values = widget.get()
314+
assert values == [
315+
datetime(year=2021, month=3, day=11),
316+
date(year=2021, month=3, day=11)
317+
]

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.