Skip to content

hue argument for xarray.plot.step() for plotting multiple histograms over shared bins #4288

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

Open
jaicher opened this issue Jul 30, 2020 · 2 comments · May be fixed by #4868
Open

hue argument for xarray.plot.step() for plotting multiple histograms over shared bins #4288

jaicher opened this issue Jul 30, 2020 · 2 comments · May be fixed by #4868

Comments

@jaicher
Copy link
Contributor

jaicher commented Jul 30, 2020

Is your feature request related to a problem? Please describe.

I love how efficiently we can plot line data for different observations using xr.DataArray.plot(hue={hue coordinate name}) over a 2D array, and I have appreciated xr.DataArray.plot.step() for plotting histogram data using interval coordinates. Today, I wanted to plot/compare several histograms over the same set of bins. I figured I could write xr.DataArray.plot.step(hue={...}), but I found out that this functionality is not implemented.

Describe the solution you'd like

I think we should have a hue kwarg for xr.DataArray.plot.step(). When specified, we would be able to plot 2D data in the same way as xr.DataArray.plot(), except that we get a set of step plots instead of a set of line plots.

Describe alternatives you've considered

  • Use xr.DataArray.plot() instead. This is effective for histograms with many bins, but inaccurately represents histograms with coarse bins
  • Manually call xr.DataArray.plot.hist() on each 1D subarray for each label on the hue coordinate, adding appropriate labels and legend. This is fine and my current solution, but I think it would be excellent to use the same shorthand that was developed for line plots.

Additional context

I didn't evaluate the other plotting functions implemented, but I suspect that others could appropriately consider a hue argument but do not yet support doing so.

@dcherian
Copy link
Contributor

I think we can add hue and faceting (row, col) support to hist. I am surprised step doesn't support hue already since iot sets a few kwargs before calling line.

Here's the error for step with hue. I think we can safely call this a bug.

~/python/xarray/xarray/plot/plot.py in step(self, *args, **kwargs)
    461     @functools.wraps(step)
    462     def step(self, *args, **kwargs):
--> 463         return step(self._da, *args, **kwargs)
    464 
    465 

~/python/xarray/xarray/plot/plot.py in step(darray, where, drawstyle, ds, *args, **kwargs)
    369     drawstyle = "steps-" + where + drawstyle
    370 
--> 371     return line(darray, *args, drawstyle=drawstyle, **kwargs)
    372 
    373 

~/python/xarray/xarray/plot/plot.py in line(darray, row, col, figsize, aspect, size, ax, hue, x, y, xincrease, yincrease, xscale, yscale, xticks, yticks, xlim, ylim, add_legend, _labels, *args, **kwargs)
    297     # Remove pd.Intervals if contained in xplt.values and/or yplt.values.
    298     xplt_val, yplt_val, xlabel, ylabel, kwargs = _resolve_intervals_1dplot(
--> 299         xplt.values, yplt.values, xlabel, ylabel, kwargs
    300     )
    301 

~/python/xarray/xarray/plot/utils.py in _resolve_intervals_1dplot(xval, yval, xlabel, ylabel, kwargs)
    509 
    510         # Convert intervals to double points
--> 511         if _valid_other_type(np.array([xval, yval]), [pd.Interval]):
    512             raise TypeError("Can't step plot intervals against intervals.")
    513         if _valid_other_type(xval, [pd.Interval]):

ValueError: could not broadcast input array from shape (100,7) into shape (100)

@jaicher PRs to fix these are very welcome!

@stale
Copy link

stale bot commented Apr 17, 2022

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

@stale stale bot added the stale label Apr 17, 2022
@dcherian dcherian removed the stale label Apr 17, 2022
@max-sixty max-sixty added the bug label Apr 17, 2022
@mgunyho mgunyho mentioned this issue Aug 22, 2022
3 tasks
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.

3 participants