Matplotlib.contourf levels depend on the number of colors in the color palette?

I draw maps using the outline, and I usually go with the default colors of flowers (rainbow) with levels = 50.

#Various imports #LOTS OF OTHER CODE BEFORE plot = plt.contourf(to_plot, 50) plt.show() #LOTS OF OTHER CODE AFTER 

The output is below. I do various other things to get coastlines, etc. This is done using iris and potatoes, if anyone is interested.

This is the result

Now I decided that I do not want to use the rainbow scheme, so I use the colors of Cyntia Brewer:

 brewer_cmap = mpl.cm.get_cmap('brewer_Reds_09') plot = iplt.contourf(to_plot, 50, cmap=brewer_cmap) # expect 50 levels 

However, the conclusion: This is the result

You can see here that this palette has only 9 colors. So my question is: are the outline levels limited by the number of colors available in the color palette? I really like this card, and I wonder if it is possible to create such a new one, but with a more red color?

I'm interested in being able to capture data variability, so more contour levels seem like a good idea, but I'm interested in losing the rainbow pattern and just moving on to one based on one color.

Hooray!

+6
source share
1 answer

Yes, this is a discrete color palette, and if you want to have a continuous set, you need to create an individual color palette.

 #the colormap data can be found here: https://github.com/SciTools/iris/blob/master/lib/iris/etc/palette/sequential/Reds_09.txt In [22]: %%file temp.txt 1.000000 0.960784 0.941176 0.996078 0.878431 0.823529 0.988235 0.733333 0.631373 0.988235 0.572549 0.447059 0.984314 0.415686 0.290196 0.937255 0.231373 0.172549 0.796078 0.094118 0.113725 0.647059 0.058824 0.082353 0.403922 0.000000 0.050980 Overwriting temp.txt In [23]: c_array = np.genfromtxt('temp.txt') from matplotlib.colors import LinearSegmentedColormap plt.register_cmap(name='Test', data={key: tuple(zip(np.linspace(0,1,c_array.shape[0]), c_array[:,i], c_array[:,i])) for key, i in zip(['red','green','blue'], (0,1,2))}) In [24]: plt.contourf(X, Y, Z, 50, cmap=plt.get_cmap('Test')) plt.colorbar() Out[24]: <matplotlib.colorbar.Colorbar instance at 0x108948320> 

enter image description here

+3
source

Source: https://habr.com/ru/post/973441/


All Articles