matplotlib 绘制3D热图

e3bfsja2  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(184)

我有一个大小为(100,519,492)的3D数组。我想绘制一个3D热图,其中颜色由数组值定义,位置由数组中的索引定义。我试着在谷歌上搜索,但没有得到任何结果。
我基本上想绘制这个2D数据集100次,形成基于数组值着色的区域(在本例中,该值被设置为0-白色或红色-1):

我试着遵循这个指南:https://www.geeksforgeeks.org/3d-heatmap-in-python/,但我得到了

ValueError: shape mismatch: objects cannot be broadcast to a single shape.  Mismatch is between arg 0 with shape (492,) and arg 1 with shape (519,).
# importing required libraries
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from pylab import *

x = np.arange(0,492) #Number of columns in my 3d data array
y = np.arange(0,519) # Number of rows
z = np.arange(0,100) # Number of 2D slices making the 3D array
colo = ODS_diff # ODS_diff is the 3D array
  
# creating figures
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection='3d')
  
# setting color bar
color_map = cm.ScalarMappable(cmap=cm.Greens_r)
color_map.set_array(colo)
  
# creating the heatmap
ax.scatter(x, y, z, marker='s',
                 s=200, color='red')
plt.colorbar(colo)
  
# adding title and labels
ax.set_title("3D Heatmap")
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
  
# displaying plot
plt.show()
iih3973s

iih3973s1#

你(或者说你的推荐人)过于复杂了

import matplotlib.pyplot as plt
import numpy as np

# different from yours, see below
x = y = z = np.linspace(-2, 2, 41)
X, Y, Z = np.meshgrid(x, y, z)
values = 2*X*X - Y*Y + 1/(Z*Z+1)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
scatter = ax.scatter(X, Y, Z, c=C, cmap='PRGn')
fig.colorbar(scatter, ax=ax)

plt.show()

这就是说(“如果你问对了人,这很容易”),我必须对此发表评论

x = np.arange(0,492) #Number of columns in my 3d data array
y = np.arange(0,519) # Number of rows
z = np.arange(0,100) # Number of 2D slices making the 3D array

对于(41, 41, 41)形状的网格,点/球体的密度已经太大了,首先要花几秒钟来渲染,当我尝试旋转或缩放时甚至更多。想象一下,当你想表示约64000个点时会发生什么。
您可能希望对网格的一个子集进行采样。

相关问题