Skip to content

Commit 6d90ab4

Browse files
committed
Convert axis limit units in Qt plot options widget
Previously axis limits were converted to floats regardless of the actual value type in Qt backend's plot options widget. This commit makes datetime axis limits get converted to actual datetime values which are considerably easier to edit. Also, the methods used to convert the datetime values back have different names under PySide2 and PyQt. Previously the options widget was using the methods provided by PyQt only. We now support both Qt bindings. Re matplotlib#19075
1 parent 1574b5e commit 6d90ab4

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

lib/matplotlib/backends/qt_editor/_formlayout.py

Lines 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

Lines 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>"),
@@ -52,8 +60,6 @@ def figure_edit(axes, parent=None):
5260
]
5361

5462
# Save the unit data
55-
xconverter = axes.xaxis.converter
56-
yconverter = axes.yaxis.converter
5763
xunits = axes.xaxis.get_units()
5864
yunits = axes.yaxis.get_units()
5965

0 commit comments

Comments
 (0)