2015-11-22 102 views
0

我有這個情節波紋管,我想離散0到20之間的顏色映射。任何人都可以幫忙嗎?Matplotlib將給定值之間的顏色條離散化

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

你能解釋一下「離散」的含義嗎?也許你想要'plt.contourf'? –

回答

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

相關問題