如何使用 matplotlib 绘制像像素图像序列一样的 3d 数组 [英] How to plot a 3d array like a image sequence of pixels with matplotlib
本文介绍了如何使用 matplotlib 绘制像像素图像序列一样的 3d 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试显示3d数组-基本上是2d图像的时间序列-像这样:
I am trying to display a 3d array - basically, a time-sequence of 2d images - like this :
基于我在SO上找到的一些代码,到目前为止,我最近的解决方案为我提供了这一功能(使用散点图和大平方标记):
Based on some code I found on SO, my closest solution so far gives me this (using scatter plot and big squared markers) :
,但方形标记未沿3轴对齐.所以我问:
-如果它们是使标记对齐(并且在绘图旋转时仍保持不变)的解决方法,
- 或/和如果他们更漂亮的方式?
but the squared markers are not aligned along the 3 axes. So I am asking:
- if their is a workaround to make the markers aligned (and still as the plot is rotated)
- or/and if their is prettier way ?
代码如下:
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
推荐答案
这是我满意的结果,按照建议使用体素:
Here's the result I'm satisfied with, using voxels as suggested :
%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()
这篇关于如何使用 matplotlib 绘制像像素图像序列一样的 3d 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文