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 28bc96f

Browse filesBrowse files
dopplershiftMeeseeksDev[bot]
authored andcommitted
Backport PR #12468: Fix set_ylim unit handling
1 parent 2e10154 commit 28bc96f
Copy full SHA for 28bc96f

File tree

Expand file treeCollapse file tree

2 files changed

+35
-11
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-11
lines changed

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3461,6 +3461,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
34613461
raise TypeError('Cannot pass both `ymax` and `top`')
34623462
top = ymax
34633463

3464+
self._process_unit_info(ydata=(bottom, top))
34643465
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
34653466
top = self._validate_converted_limits(top, self.convert_yunits)
34663467

‎lib/matplotlib/tests/test_units.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_units.py
+34-11Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import datetime
99
import platform
10+
import pytest
1011

1112

1213
# Basic class that wraps numpy array and has units
@@ -38,12 +39,8 @@ def __array__(self):
3839
return np.asarray(self.magnitude)
3940

4041

41-
# Tests that the conversion machinery works properly for classes that
42-
# work as a facade over numpy arrays (like pint)
43-
@image_comparison(baseline_images=['plot_pint'],
44-
tol={'aarch64': 0.02}.get(platform.machine(), 0.0),
45-
extensions=['png'], remove_text=False, style='mpl20')
46-
def test_numpy_facade():
42+
@pytest.fixture
43+
def quantity_converter():
4744
# Create an instance of the conversion interface and
4845
# mock so we can check methods called
4946
qc = munits.ConversionInterface()
@@ -60,12 +57,29 @@ def convert(value, unit, axis):
6057
else:
6158
return Quantity(value, axis.get_units()).to(unit).magnitude
6259

60+
def default_units(value, axis):
61+
if hasattr(value, 'units'):
62+
return value.units
63+
elif np.iterable(value):
64+
for v in value:
65+
if hasattr(v, 'units'):
66+
return v.units
67+
return None
68+
6369
qc.convert = MagicMock(side_effect=convert)
6470
qc.axisinfo = MagicMock(side_effect=lambda u, a: munits.AxisInfo(label=u))
65-
qc.default_units = MagicMock(side_effect=lambda x, a: x.units)
71+
qc.default_units = MagicMock(side_effect=default_units)
72+
return qc
6673

74+
75+
# Tests that the conversion machinery works properly for classes that
76+
# work as a facade over numpy arrays (like pint)
77+
@image_comparison(baseline_images=['plot_pint'],
78+
tol={'aarch64': 0.02}.get(platform.machine(), 0.0),
79+
extensions=['png'], remove_text=False, style='mpl20')
80+
def test_numpy_facade(quantity_converter):
6781
# Register the class
68-
munits.registry[Quantity] = qc
82+
munits.registry[Quantity] = quantity_converter
6983

7084
# Simple test
7185
y = Quantity(np.linspace(0, 30), 'miles')
@@ -79,9 +93,9 @@ def convert(value, unit, axis):
7993
ax.yaxis.set_units('inches')
8094
ax.xaxis.set_units('seconds')
8195

82-
assert qc.convert.called
83-
assert qc.axisinfo.called
84-
assert qc.default_units.called
96+
assert quantity_converter.convert.called
97+
assert quantity_converter.axisinfo.called
98+
assert quantity_converter.default_units.called
8599

86100

87101
# Tests gh-8908
@@ -97,6 +111,15 @@ def test_plot_masked_units():
97111
ax.plot(data_masked_units)
98112

99113

114+
def test_empty_set_limits_with_units(quantity_converter):
115+
# Register the class
116+
munits.registry[Quantity] = quantity_converter
117+
118+
fig, ax = plt.subplots()
119+
ax.set_xlim(Quantity(-1, 'meters'), Quantity(6, 'meters'))
120+
ax.set_ylim(Quantity(-1, 'hours'), Quantity(16, 'hours'))
121+
122+
100123
@image_comparison(baseline_images=['jpl_bar_units'], extensions=['png'],
101124
savefig_kwarg={'dpi': 120}, style='mpl20')
102125
def test_jpl_bar_units():

0 commit comments

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