Matplotlib 中生成热图
可以使用 Matplotlib 和 numpy 来创建热图。
热图示例
histogram2d
函数可以用来生成热图。
我们创建一些随机数据数组 (x, y)
以在程序中使用。我们将 bin
设置为 64,那热图将为 64×64
。你可以更改 bins
数字,如果你想要另一个尺寸的话。
import numpy as np
import numpy.random
import matplotlib.pyplot as plt
# Create data
x = np.random.randn(4096)
y = np.random.randn(4096)
# Create heatmap
heatmap, xedges, yedges = np.histogram2d(x, y, bins=(64,64))
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
# Plot heatmap
plt.clf()
plt.title('Pythonspot.com heatmap example')
plt.ylabel('y')
plt.xlabel('x')
plt.imshow(heatmap, extent=extent)
plt.show()
结果:
)
此示例中的数据点是完全随机的,使用 np.random.randn()
来生成。