如何使用Matplotlib缩放体素尺寸?

时间:2019-06-25 11:22:59

标签: python matplotlib voxel

想用Matplotlib缩放体素尺寸。我该怎么办?

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

fig = plt.figure()
ax = fig.gca(projection='3d')
# Make grid
test2 = np.zeros((6, 6, 6))
# Activate single Voxel
test2[1, 0, 4] = True

ax.voxels(test2, edgecolor="k")
ax.set_xlabel('0 - Dim')
ax.set_ylabel('1 - Dim')
ax.set_zlabel('2 - Dim')

plt.show()

将体素置于位置(1,0,4)。我想将其缩放为(0.5,0,2)。

2 个答案:

答案 0 :(得分:1)

您可以将自定义坐标传递给voxels函数:API reference

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

fig = plt.figure()
ax = fig.gca(projection='3d')
# Make grid
test2 = np.zeros((6, 6, 6))
# Activate single Voxel
test2[1, 0, 4] = True

# Custom coordinates for grid
x,y,z = np.indices((7,7,7))/2

# Pass the custom coordinates as extra arguments
ax.voxels(x, y, z, test2, edgecolor="k")
ax.set_xlabel('0 - Dim')
ax.set_ylabel('1 - Dim')
ax.set_zlabel('2 - Dim')

plt.show()

哪个会产生:

enter image description here

答案 1 :(得分:0)

voxels接受将体素放置在其上的网格的坐标。

  

voxels([x, y, z, ]/, filled, ...)

     

x, y, z:3D np.array,可选
  体素角的坐标。这应该广播成每个维度都比填充的形状大的形状。这些可用于绘制非立方体素。

     

如果未指定,则默认为沿每个轴递增整数,例如indices()返回的整数。如功能签名中的/所示,这些参数只能在位置传递。

在这种情况下

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

fig = plt.figure()
ax = fig.gca(projection='3d')
# Make grid
voxels = np.zeros((6, 6, 6))
# Activate single Voxel
voxels[1, 0, 4] = True

x,y,z = np.indices(np.array(voxels.shape)+1)

ax.voxels(x*0.5, y, z, voxels, edgecolor="k")
ax.set_xlabel('0 - Dim')
ax.set_ylabel('1 - Dim')
ax.set_zlabel('2 - Dim')

plt.show()