5
5
import matplotlib .units as munits
6
6
import numpy as np
7
7
import platform
8
+ import pytest
8
9
9
10
10
11
# Basic class that wraps numpy array and has units
@@ -36,12 +37,8 @@ def __array__(self):
36
37
return np .asarray (self .magnitude )
37
38
38
39
39
- # Tests that the conversion machinery works properly for classes that
40
- # work as a facade over numpy arrays (like pint)
41
- @image_comparison (baseline_images = ['plot_pint' ],
42
- tol = {'aarch64' : 0.02 }.get (platform .machine (), 0.0 ),
43
- extensions = ['png' ], remove_text = False , style = 'mpl20' )
44
- def test_numpy_facade ():
40
+ @pytest .fixture
41
+ def quantity_converter ():
45
42
# Create an instance of the conversion interface and
46
43
# mock so we can check methods called
47
44
qc = munits .ConversionInterface ()
@@ -58,12 +55,29 @@ def convert(value, unit, axis):
58
55
else :
59
56
return Quantity (value , axis .get_units ()).to (unit ).magnitude
60
57
58
+ def default_units (value , axis ):
59
+ if hasattr (value , 'units' ):
60
+ return value .units
61
+ elif np .iterable (value ):
62
+ for v in value :
63
+ if hasattr (v , 'units' ):
64
+ return v .units
65
+ return None
66
+
61
67
qc .convert = MagicMock (side_effect = convert )
62
68
qc .axisinfo = MagicMock (side_effect = lambda u , a : munits .AxisInfo (label = u ))
63
- qc .default_units = MagicMock (side_effect = lambda x , a : x .units )
69
+ qc .default_units = MagicMock (side_effect = default_units )
70
+ return qc
64
71
72
+
73
+ # Tests that the conversion machinery works properly for classes that
74
+ # work as a facade over numpy arrays (like pint)
75
+ @image_comparison (baseline_images = ['plot_pint' ],
76
+ tol = {'aarch64' : 0.02 }.get (platform .machine (), 0.0 ),
77
+ extensions = ['png' ], remove_text = False , style = 'mpl20' )
78
+ def test_numpy_facade (quantity_converter ):
65
79
# Register the class
66
- munits .registry [Quantity ] = qc
80
+ munits .registry [Quantity ] = quantity_converter
67
81
68
82
# Simple test
69
83
y = Quantity (np .linspace (0 , 30 ), 'miles' )
@@ -77,9 +91,9 @@ def convert(value, unit, axis):
77
91
ax .yaxis .set_units ('inches' )
78
92
ax .xaxis .set_units ('seconds' )
79
93
80
- assert qc .convert .called
81
- assert qc .axisinfo .called
82
- assert qc .default_units .called
94
+ assert quantity_converter .convert .called
95
+ assert quantity_converter .axisinfo .called
96
+ assert quantity_converter .default_units .called
83
97
84
98
85
99
# Tests gh-8908
@@ -95,6 +109,15 @@ def test_plot_masked_units():
95
109
ax .plot (data_masked_units )
96
110
97
111
112
+ def test_empty_set_limits_with_units (quantity_converter ):
113
+ # Register the class
114
+ munits .registry [Quantity ] = quantity_converter
115
+
116
+ fig , ax = plt .subplots ()
117
+ ax .set_xlim (Quantity (- 1 , 'meters' ), Quantity (6 , 'meters' ))
118
+ ax .set_ylim (Quantity (- 1 , 'hours' ), Quantity (16 , 'hours' ))
119
+
120
+
98
121
@image_comparison (baseline_images = ['jpl_bar_units' ], extensions = ['png' ],
99
122
savefig_kwarg = {'dpi' : 120 }, style = 'mpl20' )
100
123
def test_jpl_bar_units ():
0 commit comments