xarray
xarray copied to clipboard
Support for a coloarmap dictionary
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
- Add an argument that takes a dictionary and used the colors provided in the dictionary if the corresponding labels exist.
- 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
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).
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?
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).
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
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?
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.