Matplotlib在给定值之间离散颜色条

时间:2015-11-22 14:39:22

标签: python matplotlib

我有下面的情节,我想在0到20之间离散色图。任何人都可以帮忙吗?

enter image description here

以下是代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import BoundaryNorm
from matplotlib.ticker import MaxNLocator

epi='epi'
with open(epi, 'r') as f2:
    lines = f2.readlines()
data = [line.split() for line in lines]
a = np.array(data)
print a.shape
lat = a[:,0]
lat1=list(lat)
lat2=np.asarray(lat1).astype(float)
lon = a[:,1]
lon1=list(lon)
lon2=np.asarray(lon).astype(float)

x_space = 60
y_space = x_space*1.7
gridx = np.linspace(-8.8, -7.0, x_space)
gridy = np.linspace(38, 39.5, y_space )

grid, _, _ = np.histogram2d(lat2, lon2, bins=[gridy, gridx])
cmap = plt.get_cmap('hot_r')
plt.figure()
plt.axis((-8.8,-7.0,38.2,39))
plt.pcolormesh(gridx, gridy, grid,cmap=cmap)
plt.colorbar()
plt.show()

1 个答案:

答案 0 :(得分:1)

如果您想要粗略离散化的色彩映射表,您可以更改get_cmap来电并添加所需的不同(离散)颜色的数量:

import matplotlib.pylab as pl
import numpy as np

data = np.random.random([10,10]) * 40

hot2 = pl.cm.get_cmap('hot', 20) 

pl.figure()
pl.subplot(121)
pl.pcolormesh(data, cmap=pl.cm.hot, vmin=0, vmax=20)
pl.colorbar()
pl.subplot(122)
pl.pcolormesh(data, cmap=hot2, vmin=0, vmax=20)
pl.colorbar()

enter image description here