1
- from datetime import datetime
1
+ from datetime import datetime , timezone , timedelta
2
2
import platform
3
3
from unittest .mock import MagicMock
4
4
5
5
import matplotlib .pyplot as plt
6
6
from matplotlib .testing .decorators import check_figures_equal , image_comparison
7
7
import matplotlib .units as munits
8
+ from matplotlib .category import UnitData
8
9
import numpy as np
9
10
import pytest
10
11
@@ -127,12 +128,12 @@ def test_jpl_bar_units():
127
128
units .register ()
128
129
129
130
day = units .Duration ("ET" , 24.0 * 60.0 * 60.0 )
130
- x = [0 * units .km , 1 * units .km , 2 * units .km ]
131
- w = [1 * day , 2 * day , 3 * day ]
131
+ x = [0 * units .km , 1 * units .km , 2 * units .km ]
132
+ w = [1 * day , 2 * day , 3 * day ]
132
133
b = units .Epoch ("ET" , dt = datetime (2009 , 4 , 25 ))
133
134
fig , ax = plt .subplots ()
134
135
ax .bar (x , w , bottom = b )
135
- ax .set_ylim ([b - 1 * day , b + w [- 1 ]+ (1.001 )* day ])
136
+ ax .set_ylim ([b - 1 * day , b + w [- 1 ] + (1.001 ) * day ])
136
137
137
138
138
139
@image_comparison (['jpl_barh_units.png' ],
@@ -142,13 +143,13 @@ def test_jpl_barh_units():
142
143
units .register ()
143
144
144
145
day = units .Duration ("ET" , 24.0 * 60.0 * 60.0 )
145
- x = [0 * units .km , 1 * units .km , 2 * units .km ]
146
- w = [1 * day , 2 * day , 3 * day ]
146
+ x = [0 * units .km , 1 * units .km , 2 * units .km ]
147
+ w = [1 * day , 2 * day , 3 * day ]
147
148
b = units .Epoch ("ET" , dt = datetime (2009 , 4 , 25 ))
148
149
149
150
fig , ax = plt .subplots ()
150
151
ax .barh (x , w , left = b )
151
- ax .set_xlim ([b - 1 * day , b + w [- 1 ]+ (1.001 )* day ])
152
+ ax .set_xlim ([b - 1 * day , b + w [- 1 ] + (1.001 ) * day ])
152
153
153
154
154
155
def test_empty_arrays ():
@@ -172,3 +173,41 @@ class subdate(datetime):
172
173
173
174
fig_test .subplots ().plot (subdate (2000 , 1 , 1 ), 0 , "o" )
174
175
fig_ref .subplots ().plot (datetime (2000 , 1 , 1 ), 0 , "o" )
176
+
177
+
178
+ def test_shared_axis_quantity (quantity_converter ):
179
+ munits .registry [Quantity ] = quantity_converter
180
+ x = Quantity (np .linspace (0 , 1 , 10 ), "hours" )
181
+ y1 = Quantity (np .linspace (1 , 2 , 10 ), "feet" )
182
+ y2 = Quantity (np .linspace (3 , 4 , 10 ), "feet" )
183
+ fig , (ax1 , ax2 ) = plt .subplots (2 , 1 , sharex = 'all' , sharey = 'all' )
184
+ ax1 .plot (x , y1 )
185
+ ax2 .plot (x , y2 )
186
+ assert ax1 .xaxis .get_units () == ax2 .xaxis .get_units () == "hours"
187
+ assert ax2 .yaxis .get_units () == ax2 .yaxis .get_units () == "feet"
188
+ ax1 .xaxis .set_units ("seconds" )
189
+ ax2 .yaxis .set_units ("inches" )
190
+ assert ax1 .xaxis .get_units () == ax2 .xaxis .get_units () == "seconds"
191
+ assert ax1 .yaxis .get_units () == ax2 .yaxis .get_units () == "inches"
192
+
193
+
194
+ def test_shared_axis_datetime ():
195
+ # datetime uses dates.DateConverter
196
+ y1 = [datetime (2020 , i , 1 , tzinfo = timezone .utc ) for i in range (1 , 13 )]
197
+ y2 = [datetime (2021 , i , 1 , tzinfo = timezone .utc ) for i in range (1 , 13 )]
198
+ fig , (ax1 , ax2 ) = plt .subplots (1 , 2 , sharey = True )
199
+ ax1 .plot (y1 )
200
+ ax2 .plot (y2 )
201
+ ax1 .yaxis .set_units (timezone (timedelta (hours = 5 )))
202
+ assert ax2 .yaxis .units == timezone (timedelta (hours = 5 ))
203
+
204
+
205
+ def test_shared_axis_categorical ():
206
+ # str uses category.StrCategoryConverter
207
+ d1 = {"a" : 1 , "b" : 2 }
208
+ d2 = {"a" : 3 , "b" : 4 }
209
+ fig , (ax1 , ax2 ) = plt .subplots (1 , 2 , sharex = True , sharey = True )
210
+ ax1 .plot (d1 .keys (), d1 .values ())
211
+ ax2 .plot (d2 .keys (), d2 .values ())
212
+ ax1 .xaxis .set_units (UnitData (["c" , "d" ]))
213
+ assert "c" in ax2 .xaxis .get_units ()._mapping .keys ()
0 commit comments