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

New colormaps : Inferno, Viridis, ... #5883

Closed
ghost opened this issue Jan 19, 2016 · 13 comments
Closed

New colormaps : Inferno, Viridis, ... #5883

ghost opened this issue Jan 19, 2016 · 13 comments
Milestone

Comments

@ghost
Copy link

ghost commented Jan 19, 2016

Hi,

I wanted to try today the new exciting colormaps that come with the 1.5.0 version of Matplotlib. I quickly discovered I could not use them the same as the previous colormaps. Indeed, in the past I used to do:

import matplotlib as mpl
cmap = mpl.cm.get_cmap("jet", nb_curves)

# where cmap is LinearSegmentedColormap and from there get the colors the following way in order to
# handle them the way I wanted for a set of curves or points or arrows etc... :
colors = cmap(np.arange(nb_curves))

Now with the new colormaps I do the same:

import matplotlib as mpl
cmap = mpl.cm.get_cmap("inferno", nb_curves)

where cmap is now a ListedColormap and I cannot do what I used to do before.

My first question would be : why this difference between old and new colormaps?
Then, is there a way I could get a LinearSegmentedColormap from Inferno, Viridis, etc... ? This is really what I want.

Thanks a lot for your time and considering this issue!

@tacaswell tacaswell added this to the Critical bug fix release (1.5.2) milestone Jan 19, 2016
@tacaswell
Copy link
Member

What is your use case that you need a LinearSegmentedColormap instead of a ListedColormap? It looks like the only missing method on ListedColormap is set_gamma.

@ghost
Copy link
Author

ghost commented Feb 12, 2016

Well, it is just that in the detailed example above, cmap is now being a ListedColormap object instead of usually being a LinearSegmentedColormap. In that case I cannot do:
colors = cmap(np.arange(nb_curves))
when I use "inferno" instead of "jet".

@efiring
Copy link
Member

efiring commented Feb 12, 2016

I think this accomplishes what you want, doesn't it?

from matplotlib.cm import inferno
import numpy as np
nbcurves = 20
colors = inferno(np.arange(nbcurves))

@ghost
Copy link
Author

ghost commented Feb 12, 2016

Thank you very much for your answer. It gives back that error:

colors = inferno(np.arange(nbcurves))
TypeError: unsupported operand type(s) for -: 'dict' and 'int'

@QuLogic
Copy link
Member

QuLogic commented Feb 12, 2016

Have you tried with 1.5.1? The code from @efiring seems to work for me as well.

@ghost
Copy link
Author

ghost commented Feb 12, 2016

Yes, I am really sorry, my bad, you are right, no bug. In that case, inferno is still a ListedColormap and colors only contains the 20 first colors inside the colormap, but doesn't give you a nice gradient of colors adapted to the number of curves you have, i.e., for 20 curves with inferno I only get black curves moving towards gray. Do you see what I mean? Is there no way to get this LinearSegmentedColormap object from ListedColormap?

@efiring
Copy link
Member

efiring commented Feb 12, 2016

My mistake, but one small change fixes it. Use

colors = inferno(np.arange(20, dtype=float))

@ghost
Copy link
Author

ghost commented Feb 12, 2016

Thanks again, but it seems it still does not work, it gives me back a list of colors with the first element being black (one edge of the colormap), then the second element and the 18 others are pale yellow (the other edge of the colormap I guess).

@QuLogic
Copy link
Member

QuLogic commented Feb 12, 2016

You can try:

colors = inferno(np.linspace(0, inferno.N, nbcolors))

@QuLogic
Copy link
Member

QuLogic commented Feb 12, 2016

Hmm, no, I should probably have tested that out, since it doesn't work either...

@ghost
Copy link
Author

ghost commented Feb 12, 2016

Thanks again, I am deeply sorry to bother you with that. Yes it still does not work, I get the same result as my last message as you said.

@efiring
Copy link
Member

efiring commented Feb 12, 2016

OK, I still wasn't thinking straight. Do this:

colors = inferno(np.linspace(0, 1, nbcolors))

@ghost
Copy link
Author

ghost commented Feb 12, 2016

That is perfect! Solved :). Thank you very much for your precious help!

@ghost ghost closed this as completed Feb 12, 2016
@mdboom mdboom changed the title New colormaps : Inferno, Viridis, ... New colormaps : Inferno, Viridis, ... Feb 12, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants