Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib.contourf级别是否取决于颜色贴图中的颜色数量?_Python_Matplotlib_Colorbrewer - Fatal编程技术网

Python matplotlib.contourf级别是否取决于颜色贴图中的颜色数量?

Python matplotlib.contourf级别是否取决于颜色贴图中的颜色数量?,python,matplotlib,colorbrewer,Python,Matplotlib,Colorbrewer,我正在使用contourf绘制地图,通常使用默认的(彩虹)配色方案,级别=50 #Various imports #LOTS OF OTHER CODE BEFORE plot = plt.contourf(to_plot, 50) plt.show() #LOTS OF OTHER CODE AFTER 输出如下。我做了很多其他的事情来获得海岸线等等。如果有人感兴趣的话,我会用iris和cartopy来完成 现在我决定不使用彩虹方案,所以我使用一些Cyntia Brewer颜色: brew

我正在使用contourf绘制地图,通常使用默认的(彩虹)配色方案,级别=50

#Various imports
#LOTS OF OTHER CODE BEFORE
plot = plt.contourf(to_plot, 50)
plt.show()
#LOTS OF OTHER CODE AFTER
输出如下。我做了很多其他的事情来获得海岸线等等。如果有人感兴趣的话,我会用iris和cartopy来完成

现在我决定不使用彩虹方案,所以我使用一些Cyntia Brewer颜色:

brewer_cmap = mpl.cm.get_cmap('brewer_Reds_09')
plot = iplt.contourf(to_plot, 50, cmap=brewer_cmap) # expect 50 levels
然而,结果是:

你可以看到这个调色板只有9种颜色。所以我的问题是,contourf级别是否受到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>
#可在此处找到彩色地图数据:https://github.com/SciTools/iris/blob/master/lib/iris/etc/palette/sequential/Reds_09.txt
在[22]中:
%%文件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
覆盖temp.txt
在[23]中:
c_数组=np.genfromtxt('temp.txt'))
从matplotlib.colors导入LinearSegmentedColormap
plt.register_cmap(name='Test',data={key:tuple(zip(np.linspace(0,1,c_数组.shape[0]),c_数组[:,i],c_数组[:,i]))
对于键,zip中的i(['red'、'green'、'blue']、(0,1,2))})
在[24]中:
轮廓曲线(X,Y,Z,50,cmap=plt.get_cmap('Test'))
plt.colorbar()
出[24]:

干杯,这回答了我的问题。不过有一件事,你能解释一下它的作用吗:
data={key:tuple(zip(np.linspace(0,1,c_array.shape[0]),c_array[:,i],c_array[:,i])作为key,我在zip中(['red','green','blue'],(0,1,2))
我恐怕我的Python没有那么强大:)。在
{
是一种被称为字典理解的东西。基本上,它将生成制作
颜色映射所需的字典。看:那本字典应该是什么样子。干杯