简体   繁体   English

matplotlib.contourf级别是否取决于色彩映射中的颜色数量?

[英]Do matplotlib.contourf levels depend on the amount of colors in the colormap?

I'm plotting maps using contourf and I'd usually go with the default (rainbow) colorscheme with levels = 50. 我正在使用contourf绘制地图,我通常使用水平= 50的默认(彩虹)colorscheme。

#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 stuff to get the coastlines etc. It's done using iris and cartopy, if anyone's interested. 我做其他各种各样的东西来获得海岸线等。如果有人感兴趣,我会使用虹膜和手抄本完成。

这是结果

Now I've decided that I don't want to use a rainbow scheme so I'm using some Cyntia Brewer colours: 现在我决定我不想使用彩虹方案,所以我使用了一些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 output is: 但输出是: 这是结果

You can see Here that this palette only has 9 colours. 你可以在这里看到这个调色板只有9种颜色。 So my question is, are the contourf levels limited by the amount of available colours in the colormap? 所以我的问题是,色彩图级别是否受色彩图中可用颜色数量的限制? I quite like this map and I wonder if it'd be possible to generate a new one like it but with more levels of red? 我非常喜欢这张地图,我想知道是否有可能生成一个像它这样的新版本但是有更多级别的红色?

I'm interested in being able to capture the variability of the data so more contour levels seems like a good idea but I'm keen on losing the rainbow scheme and just going with one based on a single colour. 我有兴趣能够捕捉数据的可变性,所以更多的轮廓水平似乎是一个好主意,但我热衷于失去彩虹方案,只是采用基于单一颜色的方案。

Cheers! 干杯!

Yes, it is a discrete colormap, and if you want to have a continuos one you need to make a customized colormap. 是的,它是一个离散的色彩映射,如果你想要一个连续的色彩映射,你需要制作一个自定义的色彩映射。

#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>

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM