pydata / pydata/xarray

`colors` in Dataset plot methods

Open
#5,337 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-plotting
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

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 https://github.com/pydata/xarray/pull/5285#discussion_r632986368

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Dataset.plot.scatter and the _dsplot functions, then reproduce the documented examples for discrete and continuous hue_style using colors, c, and color. Done means the intended handling of colors is settled and consistently exposed, either supporting discrete hue_style or explicitly warning or rejecting cases where it is ignored.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, numpy, python
Domain
data-visualization
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.