我正在尝试将一些绘图代码从gnuplot移植到matplotlib,并且正在努力移植由颜色名称指定的不连续颜色贴图。 有关如何在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]
答案 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()
答案 1 :(得分:2)
custom_cmap
example from the mpl website中的方式定义这样的色彩映射。
这是一个非常接近你的色彩映射的例子:
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()