非均匀间隔数据的热图
Heatmap for nonuniformly spaced data
我想使用 matplotlib 创建一个热图,如下图所示。正如您从轴刻度中看到的那样,数据不是均匀分布的。
所以假设我们有
x = [1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 7]
y = [.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]
vals = np.random.rand(len(x), len(y))
如何在 matplotlib 中创建这样的插值图?
你应该插入丢失的数据,我在我的项目之一中使用了以下内容:
#create regular grid
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)
#interpolate missing data
rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
zi = rbf(xi, yi)
askorek 回答的稍微扩展版:
import matplotlib.pyplot as plt
import numpy as np
import scipy
def nonuniform_imshow(x, y, z, aspect=1, cmap=plt.cm.rainbow):
# Create regular grid
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)
# Interpolate missing data
rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
zi = rbf(xi, yi)
_, ax = plt.subplots(figsize=(6, 6))
hm = ax.imshow(zi, interpolation='nearest', cmap=cmap,
extent=[x.min(), x.max(), y.max(), y.min()])
ax.scatter(x, y)
ax.set_aspect(aspect)
return hm
heatmap = nonuniform_imshow(x, y, z)
plt.colorbar(heatmap)
plt.show()
我想使用 matplotlib 创建一个热图,如下图所示。正如您从轴刻度中看到的那样,数据不是均匀分布的。 所以假设我们有
x = [1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 7]
y = [.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5]
vals = np.random.rand(len(x), len(y))
如何在 matplotlib 中创建这样的插值图?
你应该插入丢失的数据,我在我的项目之一中使用了以下内容:
#create regular grid
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)
#interpolate missing data
rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
zi = rbf(xi, yi)
askorek 回答的稍微扩展版:
import matplotlib.pyplot as plt
import numpy as np
import scipy
def nonuniform_imshow(x, y, z, aspect=1, cmap=plt.cm.rainbow):
# Create regular grid
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)
# Interpolate missing data
rbf = scipy.interpolate.Rbf(x, y, z, function='linear')
zi = rbf(xi, yi)
_, ax = plt.subplots(figsize=(6, 6))
hm = ax.imshow(zi, interpolation='nearest', cmap=cmap,
extent=[x.min(), x.max(), y.max(), y.min()])
ax.scatter(x, y)
ax.set_aspect(aspect)
return hm
heatmap = nonuniform_imshow(x, y, z)
plt.colorbar(heatmap)
plt.show()