【问题标题】:How to plot a 3d array like a image sequence of pixels with matplotlib如何使用 matplotlib 绘制像像素图像序列一样的 3d 数组
【发布时间】:2019-08-20 14:37:56
【问题描述】:

我正在尝试显示 3d 数组 - 基本上是 2d 图像的时间序列 - 像这样:

根据我在 SO 上找到的一些代码,我目前最接近的解决方案给了我这个(使用散点图和大平方标记):

但方形标记未沿 3 个轴对齐。所以我问:
- 如果他们是使标记对齐的解决方法(并且仍然随着情节旋转)
- 或/和他们是否更漂亮?

代码如下:

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

N = 8

volume = np.random.rand(N, N, N)

x = np.arange(volume.shape[0])[:, None, None]
y = np.arange(volume.shape[1])[None, :, None]
z = np.arange(volume.shape[2])[None, None, :]
x, y, z = np.broadcast_arrays(x, y, z)

c = np.tile(volume.ravel()[:, None], [1, 3])

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x.ravel(),
       y.ravel(),
       z.ravel(),
       c=c,
       s=500, # marker's size
      marker="s") # squared markers

【问题讨论】:

  • 使用voxels?
  • 体素似乎可以工作,但需要“手动编码”标记颜色中的像素强度
  • 我认为您不需要手动编写任何代码。您通常只需将颜色图应用于您的数组。

标签: python matplotlib


【解决方案1】:

这是我满意的结果,按照建议使用体素:

%matplotlib qt

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

N = 4
volume = np.random.rand(N, N, N)
filled = np.ones((N, N, N), dtype=np.bool)

# repeating values 3 times for grayscale
colors = np.repeat(volume[:, :, :, np.newaxis], 3, axis=3)

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.voxels(filled, facecolors=colors, edgecolors='k')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多