Skip to content

Commit e8bf4bf

Browse files
TomNicholasshoyer
authored andcommitted
Bugfix for line plot axes (#2726)
* Fixed logic for setting line data * Added tests to check line data matches values of correct coords * Recorded bugfix for line plots * Update doc/whats-new.rst Co-Authored-By: TomNicholas <[email protected]> * Update doc/whats-new.rst Co-Authored-By: TomNicholas <[email protected]>
1 parent 620b946 commit e8bf4bf

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Bug fixes
5454
from higher frequencies to lower frequencies. Datapoints outside the bounds
5555
of the original time coordinate are now filled with NaN (:issue:`2197`). By
5656
`Spencer Clark <https://github.com/spencerkclark>`_.
57+
- Line plots with the `x` argument set to a non-dimensional coord now plot the correct data for 1D DataArrays.
58+
(:issue:`27251). By `Tom Nicholas <http://github.com/TomNicholas>`_.
5759
5860
.. _whats-new.0.11.3:
5961

xarray/plot/plot.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,22 @@ def _infer_line_data(darray, x, y, hue):
200200
'for line plots.')
201201

202202
if ndims == 1:
203-
dim, = darray.dims # get the only dimension name
204203
huename = None
205204
hueplt = None
206205
huelabel = ''
207206

208-
if (x is None and y is None) or x == dim:
209-
xplt = darray[dim]
207+
if x is not None:
208+
xplt = darray[x]
210209
yplt = darray
211210

212-
else:
213-
yplt = darray[dim]
211+
elif y is not None:
214212
xplt = darray
213+
yplt = darray[y]
214+
215+
else: # Both x & y are None
216+
dim = darray.dims[0]
217+
xplt = darray[dim]
218+
yplt = darray
215219

216220
else:
217221
if x is None and y is None and hue is None:

xarray/tests/test_plot.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import numpy as np
55
import pandas as pd
66
import pytest
7+
from numpy.testing import assert_array_equal
78

89
import xarray as xr
910
import xarray.plot as xplt
@@ -140,6 +141,20 @@ def test_1d_x_y_kw(self):
140141
with raises_regex(ValueError, 'None'):
141142
da.plot(x='z', y='f')
142143

144+
# Test for bug in GH issue #2725
145+
def test_infer_line_data(self):
146+
current = DataArray(name='I', data=np.array([5, 8]), dims=['t'],
147+
coords={'t': (['t'], np.array([0.1, 0.2])),
148+
'V': (['t'], np.array([100, 200]))})
149+
150+
# Plot current against voltage
151+
line = current.plot.line(x='V')[0]
152+
assert_array_equal(line.get_xdata(), current.coords['V'].values)
153+
154+
# Plot current against time
155+
line = current.plot.line()[0]
156+
assert_array_equal(line.get_xdata(), current.coords['t'].values)
157+
143158
def test_2d_line(self):
144159
with raises_regex(ValueError, 'hue'):
145160
self.darray[:, :, 0].plot.line()

0 commit comments

Comments
 (0)