我正在寻找一个热图,在每对空间x,y坐标上我都具有值(=热图颜色)z,但是我想用[0]和[z1]之间的z值标记z0 = 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()



我想将颜色栏和热图的颜色限制在0.0到0.4之间(因此避免在热图和0.0以及0.4以下的颜色栏值中使用)。
怎么做?谢谢

最佳答案

您可以将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)

09-07 03:07