在轴的极限上传播imshow热图

时间:2015-02-06 19:45:13

标签: python matplotlib imshow

我有一个x,y位置的数组在一个时间步,但我知道随着时间的推移这个范围将扩大(由图像范围设置)。有没有办法让网格的其余部分在0到填充之前为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])

但这导致了一个受限制的区域。我想基本上把白色空间变成黑色,直到新的x,y位置在稍后的某个时间点填充它们。

heatmap

1 个答案:

答案 0 :(得分:0)

要独立于传递到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()

enter image description here