![](/img/trans.png)
[英]Colormap is being ignored for matplotlib contourf plot with custom levels
[英]Do matplotlib.contourf levels depend on the amount of colors in the colormap?
我正在使用contourf繪制地圖,我通常使用水平= 50的默認(彩虹)colorscheme。
#Various imports
#LOTS OF OTHER CODE BEFORE
plot = plt.contourf(to_plot, 50)
plt.show()
#LOTS OF OTHER CODE AFTER
輸出如下。 我做其他各種各樣的東西來獲得海岸線等。如果有人感興趣,我會使用虹膜和手抄本完成。
現在我決定我不想使用彩虹方案,所以我使用了一些Cyntia Brewer顏色:
brewer_cmap = mpl.cm.get_cmap('brewer_Reds_09')
plot = iplt.contourf(to_plot, 50, cmap=brewer_cmap) # expect 50 levels
但輸出是:
你可以在這里看到這個調色板只有9種顏色。 所以我的問題是,色彩圖級別是否受色彩圖中可用顏色數量的限制? 我非常喜歡這張地圖,我想知道是否有可能生成一個像它這樣的新版本但是有更多級別的紅色?
我有興趣能夠捕捉數據的可變性,所以更多的輪廓水平似乎是一個好主意,但我熱衷於失去彩虹方案,只是采用基於單一顏色的方案。
干杯!
是的,它是一個離散的色彩映射,如果你想要一個連續的色彩映射,你需要制作一個自定義的色彩映射。
#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.