Skip to content

colors in Dataset plot methods #5337

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
zmoon opened this issue May 18, 2021 · 2 comments
Open

colors in Dataset plot methods #5337

zmoon opened this issue May 18, 2021 · 2 comments

Comments

@zmoon
Copy link
Contributor

zmoon commented May 18, 2021

While working on the plot method docstrings (#5285), I (and @keewis) found that the colors kwarg for the Dataset plot methods is not as useful as it could be. In some cases it is silently ignored (warning or error could be raised instead).

For Dataset.plot.scatter, I think it would make sense for a list of colors to be able to be passed to be used for discrete hue_style. Currently, the only way you can control these for discrete hue_style is if hue is numeric and you also pass levels.

My original notes on Dataset.plot.scatter:

Yeah, it seems like the colors argument is not used in ds.plot.scatter. But no error is raised, colors is just silently dropped (ax.scatter would raise AttributeError if it weren't) in most cases. I did find that you can get the marker colors to change by passing color or c with a discrete hue_style.

import xarray as xr
import numpy as np
ds = xr.tutorial.scatter_example_dataset()
ds["c"] = (ds.A.dims, np.random.choice(("r", "g", "b"), ds.A.shape))
ds.plot.scatter("A", "B", hue="c", c=(0, 0, 0))  # works but prints (doesn't raise) warning
ds.plot.scatter("A", "B", hue="c", color=(0, 0, 0))  # works

However, if you have a continuous hue_style, passing c is ignored (since this is done internally), and passing color raises ValueError since it conflicts with c.

It seems like at the moment, colors is really only intended to be used with contour(f) for levels, like how it is in Matplotlib. Maybe in the future it could be used to allow passing colors to be used in the color cycle for discrete hue_style, but it doesn't currently do that. So for now, maybe in the docstring we should note this current behavior (doing nothing or raising error).

levels is also unused in _dsplot functions, maybe could be removed actually I was able to get colors to sort of work with discrete hue_style by also providing levels, but levels only makes sense for numeric type:

ds["c2"] = (ds.A.dims, np.random.choice((1, 2, 3), ds.A.shape))
ds.plot.scatter("A", "B", hue="c2", colors=["r", "g", "b"], levels=[1, 2, 3, 4])  # works (rgb)
ds.plot.scatter("A", "B", hue="c2", hue_style="discrete", colors=["r", "g", "b"])  # `colors` does nothing

Originally posted by @zmoon in #5285 (comment)

@shoyer
Copy link
Member

shoyer commented May 19, 2021

Seaborn has the palette argument that is used for controlling hue. I wonder if they would make sense for Xarray, too. Certainly it's annoying to need to write a loop just to plot a few different colors on the same axis.

@zmoon
Copy link
Contributor Author

zmoon commented May 21, 2021

Yeah, having a kwarg that is noted to be specifically for the hue variable could make it more clear. It doesn't seem like the Dataset plot methods need to have both cmap and colors. They could be replaced by one, like palette, or keep just colors? But I guess with colors and levels you can make colormaps on the fly, which is convenient. And this does work for plot.scatter, but the hue variable has to be numeric.

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