3D Voxel Plotting with Matplotlib

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab will guide you through the process of creating a 3D voxel plot using the Matplotlib library in Python. Voxel plots are useful for visualizing 3D data in a way that is both clear and aesthetically pleasing. In this lab, we will be using the Axes3D.voxels function to create a voxel plot of two cubes and a link between them.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-49028{{"`3D Voxel Plotting with Matplotlib`"}} matplotlib/figures_axes -.-> lab-49028{{"`3D Voxel Plotting with Matplotlib`"}} python/lists -.-> lab-49028{{"`3D Voxel Plotting with Matplotlib`"}} python/tuples -.-> lab-49028{{"`3D Voxel Plotting with Matplotlib`"}} python/importing_modules -.-> lab-49028{{"`3D Voxel Plotting with Matplotlib`"}} python/numerical_computing -.-> lab-49028{{"`3D Voxel Plotting with Matplotlib`"}} python/data_visualization -.-> lab-49028{{"`3D Voxel Plotting with Matplotlib`"}} python/build_in_functions -.-> lab-49028{{"`3D Voxel Plotting with Matplotlib`"}} end

Import necessary libraries

First, we need to import the necessary libraries. In this case, we will be using Matplotlib and NumPy.

import matplotlib.pyplot as plt
import numpy as np

Prepare coordinates

Next, we will prepare the coordinates for our voxel plot. We will be creating an 8x8x8 grid of points using NumPy's indices function.

x, y, z = np.indices((8, 8, 8))

Now, we will create the two cubes and the link between them. We will do this by defining three boolean arrays that will be combined into a single boolean array. The first two arrays define the location of the cubes, while the third array defines the location of the link.

cube1 = (x < 3) & (y < 3) & (z < 3)
cube2 = (x >= 5) & (y >= 5) & (z >= 5)
link = abs(x - y) + abs(y - z) + abs(z - x) <= 2

voxelarray = cube1 | cube2 | link

Set colors

We will now set the colors for each object in the voxel plot. We will do this by creating an empty array of the same shape as the boolean array we created in Step 3, and then setting the color of each object based on its location.

colors = np.empty(voxelarray.shape, dtype=object)
colors[link] = 'red'
colors[cube1] = 'blue'
colors[cube2] = 'green'

Plot the voxel array

Finally, we can use the Axes3D.voxels function to plot the voxel array with the specified colors.

ax = plt.figure().add_subplot(projection='3d')
ax.voxels(voxelarray, facecolors=colors, edgecolor='k')

plt.show()

Summary

In this lab, we learned how to create a 3D voxel plot using the Matplotlib library in Python. We prepared the coordinates for the plot, created the cubes and link, set the colors for each object, and plotted the voxel array using the Axes3D.voxels function. Voxel plots are a useful way to visualize 3D data, and Matplotlib makes it easy to create them.

Other Python Tutorials you may like