@@ -293,15 +293,20 @@ def test_dataarray_getitem():
293
293
assert_identical (air .cf ["area_grid_cell" ], air .cell_area .reset_coords (drop = True ))
294
294
295
295
296
- @pytest .mark .parametrize ("obj" , dataarrays )
297
- def test_dataarray_plot (obj ):
296
+ def test_dataarray_plot ():
297
+
298
+ obj = airds .air
298
299
299
- rv = obj .isel (time = 1 ).cf . plot ( x = "X " , y = "Y" )
300
+ rv = obj .isel (time = 1 ).transpose ( "lon " , "lat" ). cf . plot ( )
300
301
assert isinstance (rv , mpl .collections .QuadMesh )
302
+ assert all (v > 180 for v in rv .axes .get_xlim ())
303
+ assert all (v < 200 for v in rv .axes .get_ylim ())
301
304
plt .close ()
302
305
303
- rv = obj .isel (time = 1 ).cf . plot . contourf ( x = "X " , y = "Y" )
306
+ rv = obj .isel (time = 1 ).transpose ( "lon " , "lat" ). cf . plot . contourf ( )
304
307
assert isinstance (rv , mpl .contour .QuadContourSet )
308
+ assert all (v > 180 for v in rv .axes .get_xlim ())
309
+ assert all (v < 200 for v in rv .axes .get_ylim ())
305
310
plt .close ()
306
311
307
312
rv = obj .cf .plot (x = "X" , y = "Y" , col = "T" )
@@ -316,6 +321,29 @@ def test_dataarray_plot(obj):
316
321
assert all ([isinstance (line , mpl .lines .Line2D ) for line in rv ])
317
322
plt .close ()
318
323
324
+ # set y automatically
325
+ rv = obj .isel (time = 0 , lon = 1 ).cf .plot .line ()
326
+ np .testing .assert_equal (rv [0 ].get_ydata (), obj .lat .data )
327
+ plt .close ()
328
+
329
+ # don't set y automatically
330
+ rv = obj .isel (time = 0 , lon = 1 ).cf .plot .line (x = "lat" )
331
+ np .testing .assert_equal (rv [0 ].get_xdata (), obj .lat .data )
332
+ plt .close ()
333
+
334
+ # various line plots and automatic guessing
335
+ rv = obj .cf .isel (T = 1 , Y = [0 , 1 , 2 ]).cf .plot .line ()
336
+ np .testing .assert_equal (rv [0 ].get_xdata (), obj .lon .data )
337
+ plt .close ()
338
+
339
+ # rv = obj.cf.isel(T=1, Y=[0, 1, 2]).cf.plot(hue="Y")
340
+ # np.testing.assert_equal(rv[0].get_xdata(), obj.lon.data)
341
+ # plt.close()
342
+
343
+ rv = obj .cf .isel (T = 1 , Y = [0 , 1 , 2 ]).cf .plot .line ()
344
+ np .testing .assert_equal (rv [0 ].get_xdata (), obj .lon .data )
345
+ plt .close ()
346
+
319
347
obj = obj .copy (deep = True )
320
348
obj .time .attrs .clear ()
321
349
rv = obj .cf .plot (x = "X" , y = "Y" , col = "time" )
@@ -714,3 +742,34 @@ def test_drop_dims(ds):
714
742
# Axis and coordinate
715
743
for cf_name in ["X" , "longitude" ]:
716
744
assert_identical (ds .drop_dims ("lon" ), ds .cf .drop_dims (cf_name ))
745
+
746
+
747
+ def test_possible_x_y_plot ():
748
+ from ..accessor import _possible_x_y_plot
749
+
750
+ # choose axes
751
+ assert _possible_x_y_plot (airds .air .isel (time = 1 ), "x" ) == "lon"
752
+ assert _possible_x_y_plot (airds .air .isel (time = 1 ), "y" ) == "lat"
753
+ assert _possible_x_y_plot (airds .air .isel (lon = 1 ), "y" ) == "lat"
754
+ assert _possible_x_y_plot (airds .air .isel (lon = 1 ), "x" ) == "time"
755
+
756
+ # choose coordinates over axes
757
+ assert _possible_x_y_plot (popds .UVEL , "x" ) == "ULONG"
758
+ assert _possible_x_y_plot (popds .UVEL , "y" ) == "ULAT"
759
+ assert _possible_x_y_plot (popds .TEMP , "x" ) == "TLONG"
760
+ assert _possible_x_y_plot (popds .TEMP , "y" ) == "TLAT"
761
+
762
+ assert _possible_x_y_plot (popds .UVEL .drop_vars ("ULONG" ), "x" ) == "nlon"
763
+
764
+ # choose X over T, Y over Z
765
+ def makeds (* dims ):
766
+ coords = {dim : (dim , np .arange (3 ), {"axis" : dim }) for dim in dims }
767
+ return xr .DataArray (np .zeros ((3 , 3 )), dims = dims , coords = coords )
768
+
769
+ yzds = makeds ("Y" , "Z" )
770
+ assert _possible_x_y_plot (yzds , "y" ) == "Z"
771
+ assert _possible_x_y_plot (yzds , "x" ) is None
772
+
773
+ xtds = makeds ("X" , "T" )
774
+ assert _possible_x_y_plot (xtds , "y" ) is None
775
+ assert _possible_x_y_plot (xtds , "x" ) == "X"
0 commit comments