Skip to content
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

Support for a coloarmap dictionary #8151

Open
rsatapat opened this issue Sep 6, 2023 · 6 comments
Open

Support for a coloarmap dictionary #8151

rsatapat opened this issue Sep 6, 2023 · 6 comments

Comments

@rsatapat
Copy link

rsatapat commented Sep 6, 2023

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

@keewis
Copy link
Collaborator

keewis commented Sep 15, 2023

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).

@rsatapat
Copy link
Author

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?

@keewis
Copy link
Collaborator

keewis commented Sep 15, 2023

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).

@rsatapat
Copy link
Author

rsatapat commented Sep 15, 2023

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

@keewis
Copy link
Collaborator

keewis commented Sep 15, 2023

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?

@shane-nichols
Copy link

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.

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