Skip to content

Guess the complementary dimension when only one is passed to pcolormesh #1291

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 8 commits into from
Mar 7, 2017

Conversation

vnoel
Copy link
Contributor

@vnoel vnoel commented Mar 2, 2017

@fmaussion
Copy link
Member

Seems pretty safe to me, thanks! Could you add some tests, too? (at least for proper code coverage)

A good place to put a test would be here:
https://github.com/pydata/xarray/blob/master/xarray/tests/test_plot.py#L638

(I suspect your PR brakes these tests, btw)

@fmaussion
Copy link
Member

Does anyone know why Travis doesn't start?

@vnoel
Copy link
Contributor Author

vnoel commented Mar 2, 2017

I have added a couple of exceptions to the _infer_xy_labels function and updated the tests as best I could

@fmaussion
Copy link
Member

Thanks! We'll have to wait a little bit here, as for some reason the travis tests do not run

@vnoel
Copy link
Contributor Author

vnoel commented Mar 2, 2017

thanks ! Do we need to worry about the "appveyor" build fails? The failure seems to happen somewhere unrelated

@vnoel
Copy link
Contributor Author

vnoel commented Mar 3, 2017

Can anyone knowledgeable tell me how to fix the appveyor build? It's complaining of a failing test_dask_distributed_integration_test...

@shoyer
Copy link
Member

shoyer commented Mar 3, 2017

I think I managed to restart the Travis-CI integration. It was disable for some mysterious reason. Maybe try pushing again or rebasing to see if that triggers them again.

See #1292 for the failing dask distibuted tests. Those can be safely ignored here.

Copy link
Member

@shoyer shoyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also needs a note in "whats-new.rst"

raise ValueError('cannot supply only one of x and y')
elif x is None:
if y not in darray.dims:
raise ValueError('y must be a coordinate variable')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little misleading -- I would say "y must be a coordinate variable if x is not supplied"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the logic. The user requests a y variable that is not in the coordinates. It would not work even if she supplied a valid x coordinate. Your messages suggests it would.

Maybe "y is not a coordinate variable" is more to the point (and the same for x)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mh, that's actually a good point. x and y can also be optional (e.g. 2D) coordinates. What about:

y must be a dimension coordinate if x is not supplied

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fmaussion : You are proposing the same message as @shoyer above. My comment seems still valid to me.

Why focus on 'if x is not supplied'? Why no equivalent 'if y is not supplied'? I feel like I'm missing something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the following example:

import xarray as xr
import numpy as np
lon, lat = np.meshgrid(np.linspace(-20, 20, 5), np.linspace(0, 30, 4))
da = xr.DataArray(np.arange(20).reshape(4, 5), dims=['y', 'x'],
                  coords = {'lat': (('y', 'x'), lat),
                            'lon': (('y', 'x'), lon)})
da.plot(x='lat')

Note that da.plot(x='lat', y='lon') is a valid call and that 'lat' in da.coords is True (but 'lat' in da.dims is False).

With your current PR the code snippet above would lead to the following error:

x must be a coordinate variable

Which is misleading, since 'lat' is a coordinate variable (it is not a dimension coordinate, however).

I just opened an issue to discuss these terminology problems: #1295

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for x and y in darray.coords then, instead of darray.dims?

No, I don't think this would work.

Since we will probably land on the name "coordinate variable" for the "dimension" coords, I guess we can go back to @shoyer 's initial suggestion, or change the error message to:

y must be a dimension name if x is not supplied

which has the advantage to cover all cases (also when there are unindexed dimensions).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in your code sample above, for instance, we don't want to guess that y should be "lon"? Why not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I understood :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in 2D Plots we are guaranteed to have only 2 dims, but we can have an arbitrary number of auxiliary coordinates, in an arbitrary order. In my example above checking for coords would work, but this is not always the case. See:

import xarray as xr
import numpy as np
lon, lat = np.meshgrid(np.linspace(-20, 20, 5), np.linspace(0, 30, 4))
da = xr.DataArray(np.arange(20).reshape(4, 5), dims=['y', 'x'],
                  coords = {'lat': (('y', 'x'), lat),
                            'lon': (('y', 'x'), lon),
                            'x': (('x', ), np.arange(5)),
                            'y': (('y', ), np.arange(4)),
                            })
print(da.coords)

Coordinates:
  * y        (y) int64 0 1 2 3
    lon      (y, x) float64 -20.0 -10.0 0.0 10.0 20.0 -20.0 -10.0 0.0 10.0 ...
  * x        (x) int64 0 1 2 3 4
    lat      (y, x) float64 0.0 0.0 0.0 0.0 0.0 10.0 10.0 10.0 10.0 10.0 ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks :-)

@shoyer
Copy link
Member

shoyer commented Mar 4, 2017

Tests should pass after a rebase or merge thanks to #1293.

@@ -22,6 +22,10 @@ v0.9.2 (unreleased)
Enhancements
~~~~~~~~~~~~

- When `plot()` is called on a 2D DataArray and only one dimension is
specified with `x=` or `=y`, the other dimension is now guessed. By
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: =y -> y=

@vnoel
Copy link
Contributor Author

vnoel commented Mar 6, 2017

I hope everything's ok now. Thanks for the hand-holding

Copy link
Member

@shoyer shoyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other than style, looks good to me

self.plotmethod('not_a_real_dim', 'y')
with self.assertRaisesRegexp(ValueError, 'x must be a dimension name if y is not supplied'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a character count handy, but is this at 80 or more characters? If so, please split onto another line. I think flake8 or pep8 should catch this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

79 characters. flake8 reports lots of errors but not in this code.

Copy link
Member

@fmaussion fmaussion Mar 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you double check please? git diff upstream/master | flake8 --diff checks only your addition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git had switched branch under me, sorry

@fmaussion
Copy link
Member

Good, thanks!

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

Successfully merging this pull request may close these issues.

guess the second coordinate when only one is passed to pcolormesh()
3 participants