13
13
import numpy as np
14
14
import pandas as pd
15
15
16
- from .utils import _determine_cmap_params
16
+ from .utils import _determine_cmap_params , _infer_xy_labels
17
17
from .facetgrid import FacetGrid
18
- from ..core .utils import is_uniform_spaced
19
18
20
19
21
20
# Maybe more appropriate to keep this in .utils
@@ -39,47 +38,6 @@ def _ensure_plottable(*args):
39
38
'or dates.' )
40
39
41
40
42
- def _infer_xy_labels (plotfunc , darray , x , y ):
43
- """
44
- Determine x and y labels when some are missing. For use in _plot2d
45
-
46
- darray is a 2 dimensional data array.
47
- """
48
- dims = list (darray .dims )
49
-
50
- if len (dims ) != 2 :
51
- raise ValueError ('{type} plots are for 2 dimensional DataArrays. '
52
- 'Passed DataArray has {ndim} dimensions'
53
- .format (type = plotfunc .__name__ , ndim = len (dims )))
54
-
55
- if x and x not in dims :
56
- raise KeyError ('{0} is not a dimension of this DataArray. Use '
57
- '{1} or {2} for x'
58
- .format (x , * dims ))
59
-
60
- if y and y not in dims :
61
- raise KeyError ('{0} is not a dimension of this DataArray. Use '
62
- '{1} or {2} for y'
63
- .format (y , * dims ))
64
-
65
- # Get label names
66
- if x and y :
67
- xlab = x
68
- ylab = y
69
- elif x and not y :
70
- xlab = x
71
- del dims [dims .index (x )]
72
- ylab = dims .pop ()
73
- elif y and not x :
74
- ylab = y
75
- del dims [dims .index (y )]
76
- xlab = dims .pop ()
77
- else :
78
- ylab , xlab = dims
79
-
80
- return xlab , ylab
81
-
82
-
83
41
def _easy_facetgrid (darray , plotfunc , x , y , row = None , col = None , col_wrap = None ,
84
42
aspect = 1 , size = 3 , subplot_kws = None , ** kwargs ):
85
43
"""
@@ -99,19 +57,18 @@ def _easy_facetgrid(darray, plotfunc, x, y, row=None, col=None, col_wrap=None,
99
57
def plot (darray , row = None , col = None , col_wrap = None , ax = None , rtol = 0.01 ,
100
58
subplot_kws = None , ** kwargs ):
101
59
"""
102
- Default plot of DataArray using matplotlib / pylab .
60
+ Default plot of DataArray using matplotlib.pyplot .
103
61
104
62
Calls xray plotting function based on the dimensions of
105
63
darray.squeeze()
106
64
107
- =============== =========== ===========================
108
- Dimensions Coordinates Plotting function
109
- --------------- ----------- ---------------------------
110
- 1 :py:func:`xray.plot.line`
111
- 2 Uniform :py:func:`xray.plot.imshow`
112
- 2 Irregular :py:func:`xray.plot.contourf`
113
- Anything else :py:func:`xray.plot.hist`
114
- =============== =========== ===========================
65
+ =============== ===========================
66
+ Dimensions Plotting function
67
+ --------------- ---------------------------
68
+ 1 :py:func:`xray.plot.line`
69
+ 2 :py:func:`xray.plot.pcolormesh`
70
+ Anything else :py:func:`xray.plot.hist`
71
+ =============== ===========================
115
72
116
73
Parameters
117
74
----------
@@ -156,9 +113,7 @@ def plot(darray, row=None, col=None, col_wrap=None, ax=None, rtol=0.01,
156
113
kwargs ['col_wrap' ] = col_wrap
157
114
kwargs ['subplot_kws' ] = subplot_kws
158
115
159
- indexes = (darray .indexes [dim ].values for dim in plot_dims )
160
- uniform = all (is_uniform_spaced (i , rtol = rtol ) for i in indexes )
161
- plotfunc = imshow if uniform else contourf
116
+ plotfunc = pcolormesh
162
117
else :
163
118
if row or col :
164
119
raise ValueError (error_msg )
@@ -376,7 +331,7 @@ def _plot2d(plotfunc):
376
331
377
332
@functools .wraps (plotfunc )
378
333
def newplotfunc (darray , x = None , y = None , ax = None , row = None , col = None ,
379
- col_wrap = None , xincrease = None , yincrease = None ,
334
+ col_wrap = None , xincrease = True , yincrease = True ,
380
335
add_colorbar = True , add_labels = True , vmin = None , vmax = None ,
381
336
cmap = None , center = None , robust = False , extend = None ,
382
337
levels = None , colors = None , subplot_kws = None , ** kwargs ):
@@ -416,8 +371,7 @@ def newplotfunc(darray, x=None, y=None, ax=None, row=None, col=None,
416
371
if ax is None :
417
372
ax = plt .gca ()
418
373
419
- xlab , ylab = _infer_xy_labels (plotfunc = plotfunc , darray = darray ,
420
- x = x , y = y )
374
+ xlab , ylab = _infer_xy_labels (darray = darray , x = x , y = y )
421
375
422
376
# better to pass the ndarrays directly to plotting functions
423
377
xval = darray [xlab ].values
@@ -471,7 +425,7 @@ def newplotfunc(darray, x=None, y=None, ax=None, row=None, col=None,
471
425
if add_colorbar :
472
426
cbar = plt .colorbar (primitive , ax = ax , extend = cmap_params ['extend' ])
473
427
if darray .name and add_labels :
474
- cbar .set_label (darray .name )
428
+ cbar .set_label (darray .name , rotation = 90 )
475
429
476
430
_update_axes_limits (ax , xincrease , yincrease )
477
431
@@ -480,7 +434,7 @@ def newplotfunc(darray, x=None, y=None, ax=None, row=None, col=None,
480
434
# For use as DataArray.plot.plotmethod
481
435
@functools .wraps (newplotfunc )
482
436
def plotmethod (_PlotMethods_obj , x = None , y = None , ax = None , row = None ,
483
- col = None , col_wrap = None , xincrease = None , yincrease = None ,
437
+ col = None , col_wrap = None , xincrease = True , yincrease = True ,
484
438
add_colorbar = True , add_labels = True , vmin = None , vmax = None ,
485
439
cmap = None , colors = None , center = None , robust = False ,
486
440
extend = None , levels = None , subplot_kws = None , ** kwargs ):
@@ -506,7 +460,7 @@ def plotmethod(_PlotMethods_obj, x=None, y=None, ax=None, row=None,
506
460
@_plot2d
507
461
def imshow (x , y , z , ax , ** kwargs ):
508
462
"""
509
- Image plot of 2d DataArray using matplotlib / pylab
463
+ Image plot of 2d DataArray using matplotlib.pyplot
510
464
511
465
Wraps matplotlib.pyplot.imshow
512
466
@@ -518,6 +472,11 @@ def imshow(x, y, z, ax, **kwargs):
518
472
The pixels are centered on the coordinates values. Ie, if the coordinate
519
473
value is 3.2 then the pixels for those coordinates will be centered on 3.2.
520
474
"""
475
+
476
+ if x .ndim != 1 or y .ndim != 1 :
477
+ raise ValueError ('imshow requires 1D coordinates, try using '
478
+ 'pcolormesh or contour(f)' )
479
+
521
480
# Centering the pixels- Assumes uniform spacing
522
481
xstep = (x [1 ] - x [0 ]) / 2.0
523
482
ystep = (y [1 ] - y [0 ]) / 2.0
@@ -589,7 +548,7 @@ def pcolormesh(x, y, z, ax, **kwargs):
589
548
590
549
# by default, pcolormesh picks "round" values for bounds
591
550
# this results in ugly looking plots with lots of surrounding whitespace
592
- if not hasattr (ax , 'projection' ):
551
+ if not hasattr (ax , 'projection' ) and x . ndim == 1 and y . ndim == 1 :
593
552
# not a cartopy geoaxis
594
553
ax .set_xlim (x [0 ], x [- 1 ])
595
554
ax .set_ylim (y [0 ], y [- 1 ])
0 commit comments