2016-06-01 18 views
1

gnuplotからmatplotlibにプロットコードを移植しようとしていますが、色名で指定された不連続なカラーマップを移植するのに苦労しています。 matplotlibでこれを行う方法に関する提案はありますか?gnuplotカラーマップをmatplotlibに変換する

# Establish a 3-section color palette with lower 1/4 in the blues, 
# and middle 1/2 light green to yellow, and top 1/4 reds 
set palette defined (0 'dark-blue', 0.5 'light-blue', \\ 
        0.5 'light-green', 1 'green', 1.5 'yellow', \\ 
        1.5 'red', 2 'dark-red') 
# Establish that the palette range, such that the middle green range corresponds 
# to 0.95 to 1.05 
set cbrange [0.9:1.1] 

Desired colorbar

+1

は、私は1が存在しない恐れ:

はここにあなたのカラーマップにかなり近いです例です。 cdictsと 'LinearSegmentedColormap'を見てください。しかし、RGB値を扱う必要があります(そして、私は認めなければなりません。 http://stackoverflow.com/questions/32524471/custom-colormap-in-pythonとhttp://matplotlib.org/examples/pylab_examples/custom_cmap.html –

答えて

2

バートの機能は非常にいいです。ただし、カラーマップを自分で作成したい場合は、custom_cmap example from the mpl websiteのように辞書を使ってこのようなカラーマップを定義することができます。 matplotlibのために1翻訳:

import matplotlib.pyplot as plt 
import matplotlib.colors as colors 
import numpy as np 

cdict = {'red': ((0.0, 0.0, 0.0), # From 0 to 0.25, we fade the red and green channels 
        (0.25, 0.5, 0.5), # up a little, to make the blue a bit more grey 

        (0.25, 0.0, 0.0), # From 0.25 to 0.75, we fade red from 0.5 to 1 
        (0.75, 1.0, 1.0), # to fade from green to yellow 

        (1.0, 0.5, 0.5)), # From 0.75 to 1.0, we bring the red down from 1 
             # to 0.5, to go from bright to dark red 

     'green': ((0.0, 0.0, 0.0), # From 0 to 0.25, we fade the red and green channels 
        (0.25, 0.6, 0.6), # up a little, to make the blue a bit more grey 

        (0.25, 1.0, 1.0), # Green is 1 from 0.25 to 0.75 (we add red 
        (0.75, 1.0, 1.0), # to turn it from green to yellow) 

        (0.75, 0.0, 0.0), # No green needed in the red upper quarter 
        (1.0, 0.0, 0.0)), 

     'blue': ((0.0, 0.9, 0.9), # Keep blue at 0.9 from 0 to 0.25, and adjust its 
        (0.25, 0.9, 0.9), # tone using the green and red channels 

        (0.25, 0.0, 0.0), # No blue needed above 0.25 
        (1.0, 0.0, 0.0)) 

      } 

cmap = colors.LinearSegmentedColormap('BuGnYlRd',cdict) 

data = 0.9 + (np.random.rand(8,8) * 0.2) # Data in range 0.9 to 1.1 

p=plt.imshow(data,interpolation='nearest',cmap=cmap,vmin=0.9,vmax=1.1) 

plt.colorbar(p) 

plt.show() 

enter image description here

+0

これは確かに非常に近いです。このアクティブな範囲を0から1ではなく0.9から1.1に適用することは可能ですか? –

+0

これで十分ですが、カラーマップは変更する必要はありません。データの範囲と、 'imshow'の' vmin'と 'vmax'だけを変更することができます。私は答えを編集して – tom

+0

色のチャンネルのカラーマップの構文ごとにややこしいことを説明する完璧でいい仕事です。 –

4

は、私は本当に覚えていないことができ、年間、このスクリプトを使用していたか、または私はそれを得たところ(編集は:いくつか検索した後、thisは元のようですが、 Python3では若干の変更が必要ですが)、カスタムカラーマップを素早く作成するのに多くの助けになりました。それはあなたが場所(0..1)と色を持つ辞書を単に指定することができ、その中から線形カラーマップを作成します。例えばmake_colormap({0:'w',1:'k'})は、白から黒に向かう線形カラーマップを作成します。

import numpy as np 
import matplotlib.pylab as pl 

def make_colormap(colors): 
    from matplotlib.colors import LinearSegmentedColormap, ColorConverter 
    from numpy import sort 

    z = np.array(sorted(colors.keys())) 
    n = len(z) 
    z1 = min(z) 
    zn = max(z) 
    x0 = (z - z1)/(zn - z1) 

    CC = ColorConverter() 
    R = [] 
    G = [] 
    B = [] 
    for i in range(n): 
     Ci = colors[z[i]]  
     if type(Ci) == str: 
      RGB = CC.to_rgb(Ci) 
     else: 
      RGB = Ci 
     R.append(RGB[0]) 
     G.append(RGB[1]) 
     B.append(RGB[2]) 

    cmap_dict = {} 
    cmap_dict['red'] = [(x0[i],R[i],R[i]) for i in range(len(R))] 
    cmap_dict['green'] = [(x0[i],G[i],G[i]) for i in range(len(G))] 
    cmap_dict['blue'] = [(x0[i],B[i],B[i]) for i in range(len(B))] 
    mymap = LinearSegmentedColormap('mymap',cmap_dict) 
    return mymap 

test1 = make_colormap({0.:'#40004b',0.5:'#ffffff',1.:'#00441b'}) 
test2 = make_colormap({0.:'b',0.25:'w',0.251:'g',0.75:'y',0.751:'r',1:'k'}) 

data = np.random.random((10,10)) 

pl.figure() 
pl.subplot(121) 
pl.imshow(data, interpolation='nearest', cmap=test1) 
pl.colorbar() 

pl.subplot(122) 
pl.imshow(data, interpolation='nearest', cmap=test2) 
pl.colorbar() 

enter image description here

+0

すごくいいですよ! –

+0

非常に良い。この機能は、カラーチャネルを個別に指定する複雑さを隠すのに役立ちます。有効範囲を0から1ではなく0.9から1.1に適用することは可能ですか? 'make_colormap({0: 'w'、0.5: 'k'、0.5: 'r'、1: 'b'})'のように重複した値を指定することはできますか? –

+0

「0.9--1.1」の範囲については、Tomsの回答を参照してください(ここでも同じです)。重複した値は現時点では機能しません(テストしたばかりです)。私は実用的なアプローチを使用して '({0: 'w'、0.5: 'k'、0.500000000001: 'r'、1: 'b'})'を使用しますが、関数を変更して重複する値。 – Bart

関連する問題