How to scale the voxel-dimensions with Matplotlib?
Asked Answered
W

2

1

Want to scale the voxel-dimensions with Matplotlib. How can I do this?

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

Instead the voxel on position (1,0,4). I want to scale it on (0.5,0,2).

Wept answered 25/6, 2019 at 11:22 Comment(0)
J
1

You can pass custom coordinates to the voxels function: 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()

Which would yield:

enter image description here

Jural answered 25/6, 2019 at 11:39 Comment(0)
C
0

voxels accept the coordinates of the grid onto which to place the 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.

In this case,

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()
Caracal answered 25/6, 2019 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.