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

Add a title to xarray plots with scalar coords #659

Merged
merged 7 commits into from
Sep 14, 2021
Merged

Conversation

maximlt
Copy link
Member

@maximlt maximlt commented Sep 10, 2021

Fixes #656

Plots based on xarray objects that have scalar coords (e.g. da.isel(lon=0)) will get an informative title as one would get with xarray.plot() API. The generated title is equal to xarray's title since internally xarray's formatting utilities are used. This is part of their private API but this code hasn't changed in years.

There are a couple of differences with xarray:

  1. If the data has more than two dimensions it will default to a histogram without providing it further hints. In that case xarray.plot() just sets 'Histogram' as a title, while hvplot() will set the informative title (e.g. lon = 250).
  2. Whenever widgets are generated to explore the data (groupby op) the title is not changed, it is a title generated by holoviews that is in sync with the widget's values and displays them ({label}: {value} {unit}, ...).
import xarray as xr
import hvplot.xarray  # noqa

ds_air = xr.tutorial.open_dataset("air_temperature")

# DataArray

air = ds_air.air

# Same as before, the title reflects the widgets' state
air.isel().hvplot.line()

# New title: lat = 75.0, lon = 200.0
air.isel(lat=0, lon=0).hvplot.line()

# New title: lon = 285.0
air.sel(lat=[20, 40, 60], lon=285).plot.line(hue='lat')

# New title: time = 2013-06-01T12:00:00'
air.sel(time='2013-06-01 12:00').hvplot()

# New title: lon = 285.0
air.sel(lat=[25, 50, 75], lon=285).hvplot.kde('air', alpha=0.5)

# Dataset

# New title that includes already existing scalar coords and those
# obtained by the iselection 
ds_roms.isel(s_rho=0, ocean_time=0).hvplot.scatter(x='salt', y='zeta')

Whenever threre's no groupby or grid. The title
is generated using xarray's api. It describes the coords
that have a size of 1.
@coveralls
Copy link

coveralls commented Sep 10, 2021

Coverage Status

Coverage decreased (-1.9%) to 69.669% when pulling 9d607af on coords_in_title into 5c41b81 on master.

hvplot/converter.py Outdated Show resolved Hide resolved
Copy link
Member

@jbednar jbednar left a comment

Choose a reason for hiding this comment

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

Thanks!!

Looks good. Could use some tests and also a reproducible example here in the PR's markdown.

This is part of their private API but this code hasn't changed in years.

Seems ok; if that interface does change I guess we could copy that code (and its associated license) in here, but for now at least it seems like a better bet that if xarray changes how it constructa those titles, we'd want to inherit those changes, and so it seems better to reply on the private API than to copy it here.

If the data has more than two dimensions it will default to a histogram without providing it further hints. In that case xarray.plot() just sets 'Histogram' as a title, while hvplot() will set the informative title (e.g. lon = 250).

The hvPlot behavior sounds more useful to me; feel free to propose that xarray change to match!

Whenever widgets are generated to explore the data (groupby op) the title is not changed, it is a title generated by holoviews that is in sync with the widget's values and displays them

Makes sense. When there are widgets, it's important to be able to see those widget values reflected in the title, so that we can be sure the plot actually does reflect the current widget settings. And if there are e.g. many, many dimensions but only a couple of widgets, we'd want the widget values in the title (as that's what's being explored), not the long list of other static dimension values. When there are no widgets, the only informative thing is the list of other static dimension values (long or not), so it seems reasonable to display that as a default. So I think I agree with this behavior, though I suppose I'd have to see how it worked in realistic situations to be sure my intuition is correct.

hvplot/converter.py Outdated Show resolved Hide resolved
Co-authored-by: James A. Bednar <jbednar@users.noreply.github.com>
@maximlt
Copy link
Member Author

maximlt commented Sep 10, 2021

Looks good. Could use some tests and also a reproducible example here in the PR's markdown.

I'll add a few tests and will edit the PR's message with an example.

The hvPlot behavior sounds more useful to me; feel free to propose that xarray change to match!

Yes I'll open an issue on their side. I also noticed that their code to format numbers actually doesn't work.

Makes sense. When there are widgets, it's important to be able to see those widget values reflected in the title, so that we can be sure the plot actually does reflect the current widget settings. And if there are e.g. many, many dimensions but only a couple of widgets, we'd want the widget values in the title (as that's what's being explored), not the long list of other static dimension values. When there are no widgets, the only informative thing is the list of other static dimension values (long or not), so it seems reasonable to display that as a default. So I think I agree with this behavior, though I suppose I'd have to see how it worked in realistic situations to be sure my intuition is correct.

Yes I agree with you, it's important to have the widgets values reflected in the title, even if it's a little redundant.

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.

move scalar coords to title in hvplot.xarray
3 participants