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

DataArray plotting: pyplot compat and passing the style #2837

Open
aldanor opened this issue Mar 21, 2019 · 6 comments
Open

DataArray plotting: pyplot compat and passing the style #2837

aldanor opened this issue Mar 21, 2019 · 6 comments

Comments

@aldanor
Copy link

aldanor commented Mar 21, 2019

These are two unrelated issues in one really that I've noticed while trying to plot things directly from DataArray objects.


The following works as expected, by converting DataArray to pandas first)

>>> arr.to_series().plot(style='.-')
>>> arr.to_series().plot.line(style='.-')

Passing Series to pyplot.plot() directly also works and retains index:

>>> plt.plot(arr.to_series(), '.-')

Trying to set style directly when plotting from DataArray doesn't work:

>>> arr.plot(style='.-')
AttributeError: Unknown property style
>>> arr.plot.line(style='.-')
AttributeError: Unknown property style

Passing DataArray to pyplot.plot() loses index:

>>> plt.plot(arr, '.-')
# works but loses coords; same as plot.plot(arr.values, '.-')
@dcherian
Copy link
Contributor

style is pandas-only kwarg (xarray lightly wraps matplotlib)

matplotlib only knows about numpy arrays so plt.plot(arr, ...) will act like plt.plot(arr.values, ...) by design.

the way to do what you want is using kwargs arr.plot.line(marker='.', ls='-'). plt.plot doesn't accept fmt='.-' so there's not much we can do to support that format string syntax.

Can you open a PR to improve the documentation on these points please?

@aldanor
Copy link
Author

aldanor commented Mar 21, 2019

matplotlib only knows about numpy arrays so plt.plot(arr, ...) will act like plt.plot(arr.values, ...) by design.

How does it (matplotlib) preserve Series index then?

style is pandas-only kwarg (xarray lightly wraps matplotlib)

Would it make sense to make it (DA plotting interface) a bit more pandas-compatible by supporting style? Given that it copies pandas syntax like arr.plot.line() anyway...

Also, if plot() is meant to be a thin wrapper around matplotlib, it should support positional arguments, since you can do plt.plot(x, y, '.-') just fine, but da.plot('.-') fails complaining about unexpected positional arguments.

Currently, neither of the two options above work, making DA plot interface inferior to both raw matplotlib and pandas.

@dcherian
Copy link
Contributor

How does it (matplotlib) preserve Series index then?

I think it plots assuming that the index is [0:len(da.values)].

ping @pydata/xarray for opinions on adding the style kwarg. I prefer that to the non-explicit da.plot('.-') syntax.

@aldanor
Copy link
Author

aldanor commented Mar 21, 2019

I think it plots assuming that the index is [0:len(da.values)].

Nope. It plots datetime index just fine.

@shoyer
Copy link
Member

shoyer commented Mar 22, 2019

I would support adding style keyword argument matching pandas. It's kind of weird that we let you pass it only as a positional argument currently. In fact, I think the only reason why we accept positional arguments in plot.line() at all is because I wanted to allow style strings.

@dcherian
Copy link
Contributor

@aldanor Would you like to tackle this problem and send in a PR?

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

No branches or pull requests

3 participants