matplotlib 画三维热力图
时间: 2023-04-10 19:05:17 浏览: 307
可以使用mpl_toolkits.mplot3d库中的Axes3D来绘制三维热力图,具体实现可以参考以下代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 生成数据
x = np.random.randint(0, 10, size=100)
y = np.random.randint(0, 10, size=100)
z = np.random.randint(0, 10, size=100)
# 绘制热力图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
hist, xedges, yedges = np.histogram2d(x, y, bins=10)
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()
# 设置颜色映射
colors = dz / dz.max()
cmap = plt.cm.get_cmap('cool')
rgba = cmap(colors)
# 绘制热力图
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=rgba, alpha=0.8)
# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
```
希望能对你有所帮助!
阅读全文