import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
data = np.random.rand(50, 50, 50)
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
x, y, z = np.indices(data.shape)
threshold = 0.5
ax.scatter(x[data > threshold], y[data > threshold], z[data > threshold],
c=data[data > threshold], cmap='inferno', marker='o')
ax.set_title("3D Volume Rendering")
ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
ax.set_zlabel("Z axis")
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Libraries:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
numpy: A powerful library used for numerical
computations in Python, particularly for creating and manipulating arrays.
data is a 3D numpy array of shape (50, 50, 50),
filled with random floating-point numbers between 0 and 1.
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
A figure object is created with a size of 10 by 8
inches.
x, y, z = np.indices(data.shape)
np.indices(data.shape) generates 3D arrays for the
x, y, and z indices corresponding to the shape of the data array, i.e., (50,
50, 50). This creates 3D grids for each dimension (x, y, and z).
threshold = 0.5
This is the threshold value. Any voxel (point) in
the volume with a value greater than 0.5 will be considered for rendering.
ax.scatter(x[data > threshold], y[data >
threshold], z[data > threshold], c=data[data > threshold],
cmap='inferno', marker='o')
ax.scatter: This creates a scatter plot of the 3D
data points.
ax.set_title("3D Volume Rendering")
ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
ax.set_zlabel("Z axis")
set_title: Sets the title of the plot as "3D
Volume Rendering".
plt.show()
This line displays the 3D plot to the screen.


0 Comments:
Post a Comment