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

plot_surface() wrapper #5084

Closed
johnomotani opened this issue Mar 27, 2021 · 2 comments · Fixed by #5101
Closed

plot_surface() wrapper #5084

johnomotani opened this issue Mar 27, 2021 · 2 comments · Fixed by #5101

Comments

@johnomotani
Copy link
Contributor

johnomotani commented Mar 27, 2021

Is there an xarray way to make a surface plot, like matplotlib's plot_surface()? I didn't see one on a quick skim, but expect it should be fairly easy to add, following the style for contour(), pcolormesh(), etc.? For the matplotlib version, see https://matplotlib.org/stable/gallery/mplot3d/surface3d.html.

@TomNicholas
Copy link
Member

I suggested this in #2235 but then never did it... It would be nice though.

@johnomotani
Copy link
Contributor Author

I started working on this, and it seems to be almost trivial, except that plot_surface() requires it's x and y arguments to be 2d arrays. My guess is that I want to special-case the broadcasting in newplotfunc() here

xarray/xarray/plot/plot.py

Lines 678 to 684 in ddc352f

# check if we need to broadcast one dimension
if xval.ndim < yval.ndim:
dims = darray[ylab].dims
if xval.shape[0] == yval.shape[0]:
xval = np.broadcast_to(xval[:, np.newaxis], yval.shape)
else:
xval = np.broadcast_to(xval[np.newaxis, :], yval.shape)

but it's not immediately obvious to me how this bit works:

  • Why is it only ever xval that needs broadcasting? Couldn't the function have been called with a 2d x and a 1d y?
  • Why is it OK to check if xval.shape[0] == yval.shape[0]? I'm probably missing something, but don't see how this check can work without referring to the actual dimensions of x and y - what if the single dimension of x was actually the second dimension of y, but happened to have the same size as the first dimension of y?
    My first thought was that I just want to do
x = x.broadcast_like(z)
y = y.broadcast_like(z)

actually, I think this is a bug. I'll make a new issue...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants