Skip to content

Commit 560a9a7

Browse files
committed
Merge pull request #794 from fmaussion/plot-coords
fix: transpose non-linked coordinates in 2d plots
2 parents f905289 + 83dd5a2 commit 560a9a7

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Bug fixes
5959
by the inference of the axis interval breaks. This change chooses not to modify
6060
the coordinate variables when the axes have the attribute ``projection``, allowing
6161
Cartopy to handle the extent of pcolormesh plots (:issue:`781`).
62+
- 2D plots now better handle additional coordinates which are not linked to the
63+
dimensions of ``DataArray`` (:issue:`788`).
6264

6365
.. _whats-new.0.7.1:
6466

xarray/plot/plot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ def newplotfunc(darray, x=None, y=None, ax=None, row=None, col=None,
378378
zval = darray.to_masked_array(copy=False)
379379

380380
# May need to transpose for correct x, y labels
381-
if xlab == darray.dims[0]:
381+
# xlab may be the name of a coord, we have to check for dim names
382+
if darray[xlab].dims[-1] == darray.dims[0]:
382383
zval = zval.T
383384

384385
_ensure_plottable(xval, yval)

xarray/test/test_plot.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,32 @@ def test_coord_strings(self):
588588
self.assertIn('y', self.darray.coords)
589589
self.plotmethod(y='y', x='x')
590590

591+
def test_non_linked_coords(self):
592+
# plot with coordinate names that are not dimensions
593+
self.darray.coords['newy'] = self.darray.y + 150
594+
# Normal case, without transpose
595+
self.plotfunc(self.darray, x='x', y='newy')
596+
ax = plt.gca()
597+
self.assertEqual('x', ax.get_xlabel())
598+
self.assertEqual('newy', ax.get_ylabel())
599+
# ax limits might change bewteen plotfuncs
600+
# simply ensure that these high coords were passed over
601+
self.assertTrue(np.min(ax.get_ylim()) > 100.)
602+
603+
def test_non_linked_coords_transpose(self):
604+
# plot with coordinate names that are not dimensions,
605+
# and with transposed y and x axes
606+
# This used to raise an error with pcolormesh and contour
607+
# https://github.com/pydata/xarray/issues/788
608+
self.darray.coords['newy'] = self.darray.y + 150
609+
self.plotfunc(self.darray, x='newy', y='x')
610+
ax = plt.gca()
611+
self.assertEqual('newy', ax.get_xlabel())
612+
self.assertEqual('x', ax.get_ylabel())
613+
# ax limits might change bewteen plotfuncs
614+
# simply ensure that these high coords were passed over
615+
self.assertTrue(np.min(ax.get_xlim()) > 100.)
616+
591617
def test_default_title(self):
592618
a = DataArray(easy_array((4, 3, 2)), dims=['a', 'b', 'c'])
593619
a.coords['d'] = u'foo'

0 commit comments

Comments
 (0)