2016-07-07 51 views
4

ログの比率を-3から3の範囲でプロットしようとしており、負の比率が緑色で正の値が赤色で、ログ比が0(中央)が白であることを希望しています。 matplotlibの既存のカラースキームのどれもがこのオプションを提供していないので、素早く素敵なグラデーションを出力する方法を見つけることができませんでした。pythonでヒートマップを作成するには緑色から赤色までの範囲ですか?

+0

。 – evtoh

+0

http://matplotlib.org/examples/pylab_examples/custom_cmap.html – user20160

答えて

2

LinearSegmentedColormapを使用して独自に作成することができます。赤と緑のチャンネルを上限と下限で1.0未満の値に設定して、色があまり明るくないようにしたい(ここで私は0.8を使いました)。あなたの好みに合わせて調整してください。

詳細については、matplotlibウェブサイトのcustom_cmap exampleを参照してください。

はここで働い例です:matplotlib.colors.LinearSegmentedColormapfrom_listメソッドを使用して

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() 

enter image description here

+0

緑色のカラーマップを使用することは、colourblindの人々があなたのプロットを解釈することができないため、素晴らしいアイデアではありません。 – 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() 

それが生成する以下のenter image description here

を参照してください。例えばより多くの用途および他の例についてはhttp://matplotlib.org/examples/pylab_examples/custom_cmap.htmlである。

-1

ここでは他の回答のいくつかよりもより直感的なようです。

from matplotlib.colors import LinearSegmentedColormap 
cmap=LinearSegmentedColormap.from_list('rg',["r", "w", "g"], N=256) 

enter image description here

以上の洗練されたチューニングのために:あなたは反転PiYGカラーマップ(真ん中に白とピンクに緑)のために解決でき

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) 

enter image description here

関連する問題