Skip to content

Commit 82cf7a7

Browse files
author
Joe Hamman
committed
pcolormesh is default 2d plotting method and updates to plotting docs
1 parent 4524834 commit 82cf7a7

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

doc/plotting.rst

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ Simple Example
141141
~~~~~~~~~~~~~~
142142

143143
The default method :py:meth:`xray.DataArray.plot` sees that the data is
144-
2 dimensional. If the coordinates are uniformly spaced then it
145-
calls :py:func:`xray.plot.imshow`.
144+
2 dimensional and calls :py:func:`xray.plot.pcolormesh`.
146145

147146
.. ipython:: python
148147
@@ -176,9 +175,9 @@ Xray plots data with :ref:`missing_values`.
176175
Nonuniform Coordinates
177176
~~~~~~~~~~~~~~~~~~~~~~
178177

179-
It's not necessary for the coordinates to be evenly spaced. If not, then
180-
:py:meth:`xray.DataArray.plot` produces a filled contour plot by calling
181-
:py:func:`xray.plot.contourf`.
178+
It's not necessary for the coordinates to be evenly spaced. Both
179+
:py:func:`xray.plot.pcolormesh` (default) and :py:func:`xray.plot.contourf` can
180+
produce plots with nonuniform coordinates.
182181

183182
.. ipython:: python
184183
@@ -201,6 +200,7 @@ matplotlib is available.
201200
plt.title('These colors prove North America\nhas fallen in the ocean')
202201
plt.ylabel('latitude')
203202
plt.xlabel('longitude')
203+
plt.tight_layout()
204204
205205
@savefig plotting_2d_call_matplotlib.png width=4in
206206
plt.show()
@@ -376,8 +376,8 @@ Faceted plotting supports other arguments common to xray 2d plots.
376376
hasoutliers[-1, -1, -1] = 400
377377
378378
@savefig plot_facet_robust.png height=12in
379-
g = hasoutliers.plot.imshow('lon', 'lat', col='time', col_wrap=3,
380-
robust=True, cmap='viridis')
379+
g = hasoutliers.plot.pcolormesh('lon', 'lat', col='time', col_wrap=3,
380+
robust=True, cmap='viridis')
381381
382382
FacetGrid Objects
383383
~~~~~~~~~~~~~~~~~
@@ -473,14 +473,13 @@ plotting function based on the dimensions of the ``DataArray`` and whether
473473
the coordinates are sorted and uniformly spaced. This table
474474
describes what gets plotted:
475475

476-
=============== =========== ===========================
477-
Dimensions Coordinates Plotting function
478-
--------------- ----------- ---------------------------
479-
1 :py:func:`xray.plot.line`
480-
2 Uniform :py:func:`xray.plot.imshow`
481-
2 Irregular :py:func:`xray.plot.contourf`
482-
Anything else :py:func:`xray.plot.hist`
483-
=============== =========== ===========================
476+
=============== ===========================
477+
Dimensions Plotting function
478+
--------------- ---------------------------
479+
1 :py:func:`xray.plot.line`
480+
2 :py:func:`xray.plot.pcolormesh`
481+
Anything else :py:func:`xray.plot.hist`
482+
=============== ===========================
484483

485484
Coordinates
486485
~~~~~~~~~~~

xray/plot/facetgrid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def map_dataarray(self, func, x, y, **kwargs):
274274
extend=cmap_params['extend'])
275275

276276
if self.data.name:
277-
cbar.set_label(self.data.name, rotation=270,
277+
cbar.set_label(self.data.name, rotation=90,
278278
verticalalignment='bottom')
279279

280280
self._x_var = x

xray/plot/plot.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
from .utils import _determine_cmap_params, _infer_xy_labels
1717
from .facetgrid import FacetGrid
18-
from ..core.utils import is_uniform_spaced
1918

2019

2120
# Maybe more appropriate to keep this in .utils
@@ -58,19 +57,18 @@ def _easy_facetgrid(darray, plotfunc, x, y, row=None, col=None, col_wrap=None,
5857
def plot(darray, row=None, col=None, col_wrap=None, ax=None, rtol=0.01,
5958
subplot_kws=None, **kwargs):
6059
"""
61-
Default plot of DataArray using matplotlib / pylab.
60+
Default plot of DataArray using matplotlib.pyplot.
6261
6362
Calls xray plotting function based on the dimensions of
6463
darray.squeeze()
6564
66-
=============== =========== ===========================
67-
Dimensions Coordinates Plotting function
68-
--------------- ----------- ---------------------------
69-
1 :py:func:`xray.plot.line`
70-
2 Uniform :py:func:`xray.plot.imshow`
71-
2 Irregular :py:func:`xray.plot.contourf`
72-
Anything else :py:func:`xray.plot.hist`
73-
=============== =========== ===========================
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+
=============== ===========================
7472
7573
Parameters
7674
----------
@@ -115,9 +113,7 @@ def plot(darray, row=None, col=None, col_wrap=None, ax=None, rtol=0.01,
115113
kwargs['col_wrap'] = col_wrap
116114
kwargs['subplot_kws'] = subplot_kws
117115

118-
indexes = (darray.indexes[dim].values for dim in plot_dims)
119-
uniform = all(is_uniform_spaced(i, rtol=rtol) for i in indexes)
120-
plotfunc = imshow if uniform else contourf
116+
plotfunc = pcolormesh
121117
else:
122118
if row or col:
123119
raise ValueError(error_msg)
@@ -335,7 +331,7 @@ def _plot2d(plotfunc):
335331

336332
@functools.wraps(plotfunc)
337333
def newplotfunc(darray, x=None, y=None, ax=None, row=None, col=None,
338-
col_wrap=None, xincrease=None, yincrease=None,
334+
col_wrap=None, xincrease=True, yincrease=True,
339335
add_colorbar=True, add_labels=True, vmin=None, vmax=None,
340336
cmap=None, center=None, robust=False, extend=None,
341337
levels=None, colors=None, subplot_kws=None, **kwargs):
@@ -429,7 +425,7 @@ def newplotfunc(darray, x=None, y=None, ax=None, row=None, col=None,
429425
if add_colorbar:
430426
cbar = plt.colorbar(primitive, ax=ax, extend=cmap_params['extend'])
431427
if darray.name and add_labels:
432-
cbar.set_label(darray.name)
428+
cbar.set_label(darray.name, rotation=90)
433429

434430
_update_axes_limits(ax, xincrease, yincrease)
435431

@@ -438,7 +434,7 @@ def newplotfunc(darray, x=None, y=None, ax=None, row=None, col=None,
438434
# For use as DataArray.plot.plotmethod
439435
@functools.wraps(newplotfunc)
440436
def plotmethod(_PlotMethods_obj, x=None, y=None, ax=None, row=None,
441-
col=None, col_wrap=None, xincrease=None, yincrease=None,
437+
col=None, col_wrap=None, xincrease=True, yincrease=True,
442438
add_colorbar=True, add_labels=True, vmin=None, vmax=None,
443439
cmap=None, colors=None, center=None, robust=False,
444440
extend=None, levels=None, subplot_kws=None, **kwargs):
@@ -464,7 +460,7 @@ def plotmethod(_PlotMethods_obj, x=None, y=None, ax=None, row=None,
464460
@_plot2d
465461
def imshow(x, y, z, ax, **kwargs):
466462
"""
467-
Image plot of 2d DataArray using matplotlib / pylab
463+
Image plot of 2d DataArray using matplotlib.pyplot
468464
469465
Wraps matplotlib.pyplot.imshow
470466
@@ -478,7 +474,7 @@ def imshow(x, y, z, ax, **kwargs):
478474
"""
479475

480476
if x.ndim != 1 or y.ndim != 1:
481-
raise ValueError('Imshow requires 1D coordinates, try using '
477+
raise ValueError('imshow requires 1D coordinates, try using '
482478
'pcolormesh or contour(f)')
483479

484480
# Centering the pixels- Assumes uniform spacing

xray/test/test_plot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ def test_2d_before_squeeze(self):
8989
a.plot()
9090

9191
def test2d_uniform_calls_imshow(self):
92-
self.assertTrue(self.imshow_called(self.darray[:, :, 0].plot))
92+
self.assertTrue(self.imshow_called(self.darray[:, :, 0].plot.imshow))
9393

9494
def test2d_nonuniform_calls_contourf(self):
9595
a = self.darray[:, :, 0]
9696
a.coords['dim_1'] = [2, 1, 89]
97-
self.assertTrue(self.contourf_called(a.plot))
97+
self.assertTrue(self.contourf_called(a.plot.contourf))
9898

9999
def test3d(self):
100100
self.darray.plot()
@@ -707,7 +707,7 @@ def test_imshow_called(self):
707707
self.assertTrue(self.imshow_called(self.darray.plot.imshow))
708708

709709
def test_xy_pixel_centered(self):
710-
self.darray.plot.imshow()
710+
self.darray.plot.imshow(yincrease=None)
711711
self.assertTrue(np.allclose([-0.5, 14.5], plt.gca().get_xlim()))
712712
self.assertTrue(np.allclose([9.5, -0.5], plt.gca().get_ylim()))
713713

0 commit comments

Comments
 (0)