xarray icon indicating copy to clipboard operation
xarray copied to clipboard

Support for a coloarmap dictionary

Open rsatapat opened this issue 1 year ago • 6 comments

Is your feature request related to a problem?

When I plot multiple lines using xarray.plot.line(), I would like xarray to be able to get the colors from a dictionary and use that for the color of the lines.

Describe the solution you'd like

There are 2 functionalities that can solve this problem

  1. Add an argument that takes a dictionary and used the colors provided in the dictionary if the corresponding labels exist.
  2. Make it possible to add a colormap dictionary to attributes and xarray.plot uses it whenever a color corresponding to the dimension exists.

Describe alternatives you've considered

No response

Additional context

No response

rsatapat avatar Sep 06 '23 14:09 rsatapat

did you try creating a matplotlib colormap instance from your dictionary, then passing that? According to the docstring those are accepted as well, so this should work already (if not, that would be a bug).

keewis avatar Sep 15 '23 14:09 keewis

did you try creating a matplotlib colormap instance from your dictionary, then passing that? According to the docstring those are accepted as well, so this should work already (if not, that would be a bug).

I am a little confused. From the docstring, it does not look like plot.line() takes a colormap as an input. Am I missing something?

rsatapat avatar Sep 15 '23 19:09 rsatapat

it does not, so that's my bad.

Could you post (a part of) the dictionary you had in mind? That way, I can actually try things out without guessing (a educated guess, but a guess nonetheless).

keewis avatar Sep 15 '23 19:09 keewis

Its a recurring issue. I'll just give an example of what I had in mind.

da = xr.DataArray(data= np.random.rand(4, 3),
dims=("x", "y"),
coords={"x": [10, 20, 30, 40], "y": [1, 2, 3]})

colormap_dict = {'1':'r', '2':'g', '3':'k'}
da.plot.line(x="x", hue="y", colormap=colormap_dict)

OR da.plot.line(x="x", hue="y", colormap="viridis") And the colors are chosen from the supplied colormap.

If I am not mistaken, the way to do this currently, would be to set a color cycle using plt.cycler() which is reasonable. But, given the powerful plotting functionality of xarray, this would be a nice addition

rsatapat avatar Sep 15 '23 19:09 rsatapat

great, thanks. We're currently using matplotlib.axes.Axes.plot for this underneath, so if we can figure out how to do this with just matplotlib but without the global settings that might make things easier to decide.

@Illviljan (or any others who have worked on plotting a bit), any opinions? Do we have any prior art in pandas for this?

keewis avatar Sep 15 '23 20:09 keewis

So pandas.DataFrame.plot() has an argument 'cmap' that applies a named colormap. I often have to do DataArray.to_pandas().plot(cmap='plasma') to apply colormaps as xarray.plot.line does not have this feature. It is an ugly workaround. Can't predict if I need to transpose. Axes don't get labeled... Also, formatting of legends in xarray is bad. No floating point formatting for some reason. Pandas does that as well.

shane-nichols avatar Mar 05 '24 21:03 shane-nichols