Skip to content

colors in Dataset plot methods #5337

Open
@zmoon

Description

@zmoon

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions