我正在寻找一个热图,我在每个空间x,y坐标处都有值(=热图颜色)z,但我想用z0 =标记[z0,z1]之间的z值0.0和z1 = 0.4,而一些内插z值低于和高于这些边界。
from numpy.random import uniform, seed
from matplotlib.mlab import griddata
import matplotlib.pyplot as plt
import numpy as np
# make up data.
#npts = int(raw_input('enter # of random points to plot:'))
seed(0)
npts = 200
x = uniform(-2, 2, npts)
y = uniform(-2, 2, npts)
z = x*np.exp(-x**2 - y**2)
# define grid.
xi = np.linspace(0, 1, 1000)
yi = np.linspace(0, 1, 1000)
# grid the data.
zi = griddata(x, y, z, xi, yi, interp='linear')
# contour the gridded data, plotting dots at the nonuniform data points.
CS = plt.contourf(xi, yi, zi, 15, cmap=plt.cm.rainbow,
vmax=abs(zi).max(), vmin=-abs(zi).max())
plt.colorbar() # draw colorbar
# plot data points.
plt.show()
我想将colorbar和heatmap颜色限制在0.0到0.4之间(因此请避免在热图和颜色条中的值低于0.0且高于0.4)。 怎么做?感谢
答案 0 :(得分:2)
您可以将numpy数组中的值设置为None
,使其保持未开发状态。例如,
zmin = 0.0
zmax = 0.4
zi[(zi<zmin) | (zi>zmax)] = None
CS = plt.contourf(xi, yi, zi, 15, cmap=plt.cm.rainbow,
vmax=zmax, vmin=zmin)