绘制热图的方法

绘制热图的方法

# 在三维图上绘制多个平行的二维热图
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 创建一个三维矩阵,例如,一个10x10x5的矩阵
matrix_3d = np.random.rand(10, 10, 5)

# 获取矩阵的两个空间维度
x = np.linspace(0, 1, matrix_3d.shape[0])
y = np.linspace(0, 1, matrix_3d.shape[1])
X, Y = np.meshgrid(x, y)

# 创建一个图形和一个子图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制多个平行的二维热图
z_positions = np.linspace(0, 1, matrix_3d.shape[2])  # Z轴位置
for i in range(matrix_3d.shape[2]):
    Z = matrix_3d[:, :, i]
    color = ax.contourf(X, Y, Z, zdir='z', offset=z_positions[i], cmap='viridis', alpha=0.5)

# 添加颜色条
fig.colorbar(color, ax=ax, pad=0.1)

# 设置坐标轴标签
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')

# 显示图形
plt.show()

在这里插入图片描述

# 绘制三维曲面的热图
# import numpy as np
# import matplotlib.pyplot as plt
# from mpl_toolkits.mplot3d import Axes3D
#
# # 创建一个X, Y值的网格
# x = np.linspace(-5, 5, 100)
# y = np.linspace(-5, 5, 100)
# X, Y = np.meshgrid(x, y)
#
# # 创建一个Z值的矩阵,这里使用一个简单的函数作为示例
# Z = np.sin(np.sqrt(X**2 + Y**2))
#
# # 创建一个图形和一个子图
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
#
# # 绘制曲面图
# surf = ax.plot_surface(X, Y, Z, cmap='viridis')
#
# # 添加颜色条
# fig.colorbar(surf)
#
# # 设置坐标轴标签
# ax.set_xlabel('X Axis')
# ax.set_ylabel('Y Axis')
# ax.set_zlabel('Z Axis')
#
# # 显示图形
# plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值