将 imshow 热图分布在轴的范围内

spread imshow heatmap over limits of axis

我在一个时间步长有一组 x,y 位置,但我知道随着时间的推移这个范围会扩大(已经由图像范围设置)。有没有办法使范围内的其余网格在填充之前为 0。我的理解是 np.histogram2d 中的 x、y 位置或地图本身需要在不同大小的新网格上进行重组,但我不确定如何进行重组。到目前为止我有:

heatmap, xedges, yedges = np.histogram2d(x,y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
ax.imshow(heatmap.T, origin='lower',extent=extent,cmap='cubehelix')
ax.set_xlim([20,220])
ax.set_ylim([-1,1])

但这会导致区域受限。我想让白色 space 基本上变成黑色,直到新的 x,y 位置在稍后的某个时间步填充它们。

要独立于传入 numpy.histogram2d 的数据来控制直方图的范围,请将 bin 位置指定为数组。

例如:

import numpy as np
import matplotlib.pyplot as plt

# Known ranges for the histogram and plot
xmin, xmax = 20, 220
ymin, ymax = -1, 1

# Generate some random data
x = np.random.normal(48, 5, 100)
y = np.random.normal(0.4, 0.1, 100)

# Create arrays specifying the bin edges
nbins = 50
xbins = np.linspace(xmin, xmax, nbins)
ybins = np.linspace(ymin, ymax, nbins)

# Create the histogram using the specified bins
data, _, _ = np.histogram2d(x, y, bins=(xbins, ybins))

# Plot the result
fig, ax = plt.subplots()
ax.imshow(data.T, origin='lower', cmap='cubehelix', aspect='auto',
          interpolation='nearest', extent=[xmin, xmax, ymin, ymax])

ax.axis([xmin, xmax, ymin, ymax])
plt.show()