Skip to content
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

Plotting with coordinates that have different numbers of dimensions using xarray.hvplot #244

Closed
hetland opened this issue Jul 17, 2019 · 1 comment · Fixed by holoviz/holoviews#3955
Labels
type: bug Something isn't working

Comments

@hetland
Copy link

hetland commented Jul 17, 2019

There is an error in plotting coordinates with multiple dimensions using xarray.hvplot. xarray.plot (and also matplotlib) have the correct behavior. Here is some code to demonstrate the issue.

import numpy as np
import xarray as xr
import hvplot.xarray
import pandas as pd

import matplotlib.pyplot as plt
%matplotlib inline

# create an example dataset
dates = pd.date_range('2000-01-01', '2001-12-31', name='time')
times = dates - dates[0]

x = np.linspace(0, 10, 101)

h = np.linspace(3, 7, 101)
s = np.linspace(0, 1, 51)
z = s[:, np.newaxis] * h[np.newaxis, :]

data = (np.sin(x) * np.cos(z)) * np.cos(np.asarray(times.days[:, np.newaxis, np.newaxis]))

# construct xarray dataset
ds = xr.Dataset({'data': (('time', 's', 'x'), data)}, 
                {'time':dates, 'x':x, 's':s, 'z':(('s', 'x'), z)})

Matplotlib does the right thing using the raw numpy arrays:

# plot an example time slice
plt.pcolormesh(x, z, data[0])

xarray.plot also does the right thing:

ds.data.sel(time='2001-08-07').plot(x='x', y='z')

xarray.hvplot fails:

ds.hvplot.quadmesh(x='x', y='z')

Making a dummy 2d coordinate for x fixes things, and shows the functionality that users probably expect:

x2d = x*np.ones_like(z)
ds.coords['x2d'] = xr.DataArray(x2d, (ds.s, ds.x))
ds.data.hvplot.quadmesh(x='x2d', y='z', clim=(-2,2))

A simple fix could be to do a ones_like broadcast within hvplot when coordinates don't match, like presented in the last example, to ensure coordinates have the same size and shape.

@rsignell-usgs
Copy link

I'm running into this problem as well for ROMS ocean model examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants