diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 184cee05ae2..39ba72063ef 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -54,6 +54,8 @@ Bug fixes from higher frequencies to lower frequencies. Datapoints outside the bounds of the original time coordinate are now filled with NaN (:issue:`2197`). By `Spencer Clark `_. +- Line plots with the `x` argument set to a non-dimensional coord now plot the correct data for 1D DataArrays. + (:issue:`27251). By `Tom Nicholas `_. .. _whats-new.0.11.3: diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 13d6ec31104..9178dd8f031 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -200,18 +200,22 @@ def _infer_line_data(darray, x, y, hue): 'for line plots.') if ndims == 1: - dim, = darray.dims # get the only dimension name huename = None hueplt = None huelabel = '' - if (x is None and y is None) or x == dim: - xplt = darray[dim] + if x is not None: + xplt = darray[x] yplt = darray - else: - yplt = darray[dim] + elif y is not None: xplt = darray + yplt = darray[y] + + else: # Both x & y are None + dim = darray.dims[0] + xplt = darray[dim] + yplt = darray else: if x is None and y is None and hue is None: diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 529d35db865..3b08ce706f5 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -4,6 +4,7 @@ import numpy as np import pandas as pd import pytest +from numpy.testing import assert_array_equal import xarray as xr import xarray.plot as xplt @@ -140,6 +141,20 @@ def test_1d_x_y_kw(self): with raises_regex(ValueError, 'None'): da.plot(x='z', y='f') + # Test for bug in GH issue #2725 + def test_infer_line_data(self): + current = DataArray(name='I', data=np.array([5, 8]), dims=['t'], + coords={'t': (['t'], np.array([0.1, 0.2])), + 'V': (['t'], np.array([100, 200]))}) + + # Plot current against voltage + line = current.plot.line(x='V')[0] + assert_array_equal(line.get_xdata(), current.coords['V'].values) + + # Plot current against time + line = current.plot.line()[0] + assert_array_equal(line.get_xdata(), current.coords['t'].values) + def test_2d_line(self): with raises_regex(ValueError, 'hue'): self.darray[:, :, 0].plot.line()