- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Representing voxels with matplotlib

In 3D computer graphics, a voxel represents a value on a regular grid in three-dimensional space. We can say a voxel is a 3D equivalent of a pixel that is used in 2D. A pixel is a square inside of a 2D image with a position in a 2D grid and a single color value, whereas a voxel is a cube inside of a 3D model with a position inside a 3D grid and a single color value.

To represent voxels with matplotlib, we can take the following steps −

## Steps

Set the figure size and adjust the padding between and around the subplots.

Create random choice data points between 0 and 1.

Create a new figure or activate an existing figure.

Get the current axes, creating one if necessary, with

**projection='3d'**.Plot a set of filled voxels, using

**voxels()**method.To display the figure, use

**show()**method.

## Example

import numpy as np import matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Random data points between 0 and 1 data = np.random.choice([0, 1], size=(5, 7, 9), p=[0.65, 0.35]) # Create a new figure fig = plt.figure() # Axis with 3D projection ax = fig.gca(projection='3d') # Plot the voxels ax.voxels(data, edgecolor="k", facecolors='green') # Display the plot plt.show()

## Output

It will produce the following output −

- Related Articles
- How to draw a log-normalized imshow plot with a colorbar representing the raw data in Matplotlib?
- Binary String With Substrings Representing 1 To N in C++
- What is Representing Scope Information?
- Representing seconds in HH:MM:SS in JavaScript
- Plot numpy datetime64 with Matplotlib
- Linear regression with Matplotlib/Numpy
- Saving scatterplot animations with matplotlib
- Animation with contours in matplotlib
- Write the fraction representing the shaded portion."\n
- Line plot with arrows in Matplotlib
- Histogram for discrete values with Matplotlib
- Matplotlib histogram with multiple legend entries
- Legend with vertical line in matplotlib
- Python – Filter immutable rows representing Dictionary Keys from Matrix
- Python - Working with PNG Images using Matplotlib