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

How to scale the voxel-dimensions with Matplotlib?

想用 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) 上缩放它。

您可以将自定义坐标传递给 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()

这会产生:

voxels 接受放置体素的网格坐标。

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

x, y, z : 3D np.array, optional
The coordinates of the corners of the voxels. This should broadcast to a shape one larger in every dimension than the shape of filled. These can be used to plot non-cubic voxels.

If not specified, defaults to increasing integers along each axis, like those returned by indices(). As indicated by the / in the function signature, these arguments can only be passed positionally.

在这种情况下,

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()