Skip to content

Bugfix for line plot axes #2726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/spencerkclark>`_.
- 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 <http://github.com/TomNicholas>`_.

.. _whats-new.0.11.3:

Expand Down
14 changes: 9 additions & 5 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down