我試圖繪製從-3到3的範圍內的對數比,並希望負比率爲綠色,正值爲紅色,對數比例爲0(中心)爲白色。 matplotlib中沒有預先存在的顏色方案提供此選項,我還無法弄清楚如何手動輸出一個不錯的漸變。如何在python中創建一個從綠色到紅色的熱圖?
4
A
回答
2
您可以使用LinearSegmentedColormap
創建自己的。我喜歡將紅色和綠色通道的上限和下限設置爲小於1.0,所以顏色不太亮(這裏我使用了0.8)。調整以適應你的口味。
查看matplotlib網站上的custom_cmap example瞭解更多詳情。
下面是一個工作示例:
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
# This dictionary defines the colormap
cdict = {'red': ((0.0, 0.0, 0.0), # no red at 0
(0.5, 1.0, 1.0), # all channels set to 1.0 at 0.5 to create white
(1.0, 0.8, 0.8)), # set to 0.8 so its not too bright at 1
'green': ((0.0, 0.8, 0.8), # set to 0.8 so its not too bright at 0
(0.5, 1.0, 1.0), # all channels set to 1.0 at 0.5 to create white
(1.0, 0.0, 0.0)), # no green at 1
'blue': ((0.0, 0.0, 0.0), # no blue at 0
(0.5, 1.0, 1.0), # all channels set to 1.0 at 0.5 to create white
(1.0, 0.0, 0.0)) # no blue at 1
}
# Create the colormap using the dictionary
GnRd = colors.LinearSegmentedColormap('GnRd', cdict)
# Make a figure and axes
fig,ax = plt.subplots(1)
# Some fake data in the range -3 to 3
dummydata = np.random.rand(5,5)*6.-3.
# Plot the fake data
p=ax.pcolormesh(dummydata,cmap=GnRd,vmin=-3,vmax=3)
# Make a colorbar
fig.colorbar(p,ax=ax)
plt.show()
+0
我會補充一點,使用綠紅色的地圖並不是一個好主意,因爲色彩鮮豔的人們無法解釋你的情節! – tom
2
如何使用以下LinearSegmentedColormap
:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
cmapGR = LinearSegmentedColormap(
'GreenRed',
{
'red': ((0.0, 0.0, 0.0),
(0.5, 1.0, 1.0),
(1.0, 1.0, 1.0)),
'green':((0.0, 1.0, 1.0),
(0.5, 1.0, 1.0),
(1.0, 0.0, 0.0)),
'blue': ((0.0, 0.0, 0.0),
(0.5, 1.0, 1.0),
(1.0, 0.0, 0.0))
},)
plt.imshow(np.array([np.arange(200) for i in range(200)]), cmap=cmapGR)
plt.show()
見例如http://matplotlib.org/examples/pylab_examples/custom_cmap.html更多用途和其他例子。
-1
使用matplotlib.colors.LinearSegmentedColormap
的from_list
方法似乎比這裏的一些其他的答案更直觀。
from matplotlib.colors import LinearSegmentedColormap
cmap=LinearSegmentedColormap.from_list('rg',["r", "w", "g"], N=256)
或爲更復雜的調整:
from matplotlib.colors import LinearSegmentedColormap
c = ["darkred","red","lightcoral","white", "palegreen","green","darkgreen"]
v = [0,.15,.4,.5,0.6,.9,1.]
l = list(zip(v,c))
cmap=LinearSegmentedColormap.from_list('rg',l, N=256)
相關問題
- 1. 在圖像上創建紅色和綠色點
- 2. 漂浮在一個矩陣顏色紅色到綠色
- 3. 值紅色/黃色/綠色(如何?)
- 4. 顏色從綠色到紅色與百分比
- 5. HTML顏色代碼:紅色到黃色到綠色
- 6. 處理js紅色()綠色()藍色()
- 7. 如何將JPG圖像讀入3個矩陣:紅色,綠色,藍色?
- 8. 如何將紅色添加到綠色並變黑?
- 9. osx應用程序紅色橙色綠色圖標
- 10. 阿爾法混合紅色,藍色和綠色圖像產生一個圖像有色任何rgb值?
- 11. 地塊所有紅色和綠色
- 12. .Net Reflector紅色和綠色方塊
- 13. 什麼是紅色/綠色測試?
- 14. 將綠色轉換爲紅色
- 15. 如何從紅色改變的UITableViewCell的背景顏色爲橙色到綠色順利
- 16. 的Android創建自定義的UI對象,紅色/綠色連接按鈕
- 17. 如何在畫布中填充不同顏色的所有圈子:紅色,黃色,藍色和綠色?
- 18. 從Android顏色對象獲取紅色,藍色或綠色通道
- 19. 如何從http://cmsaspnet.com/forum.aspx頁面創建一個像按鈕一樣的白色綠色按鈕?
- 20. 在pygame中,你如何使背景在一個循環中不斷改變紅色,綠色,藍色,黑色和白色之間的顏色
- 21. 轉換圖像爲3d綠色洋紅色眼鏡
- 22. 從綠盤Quantmod ChartSeries中線條顏色到另一種顏色
- 23. 當語法(iePURPLE或PURPAL)錯誤時,如何從csv文件分組顏色(藍色,綠色,紫色,紅色)?
- 24. 如何在mediawiki中創建一些文字爲紅色?
- 25. Java BufferedImage單獨獲取紅色,綠色和藍色
- 26. 使用Color.brighter方法打印紅色,綠色和藍色值
- 27. RGBApixel'沒有成員名爲...紅色藍色和綠色
- 28. jquery下拉列表都獲得顏色綠色時selecet紅色
- 29. PHP合併兩個圖像創建一個粉紅色光
- 30. 紅色綠燈測試的相關性
你可以勉強接受倒PiYG顏色表(綠色,中間的白色到粉紅色)。 – evtoh
http://matplotlib.org/examples/pylab_examples/custom_cmap.html – user20160