logo
  • Plot types
  • Examples
  • Tutorials
  • Reference
  • User guide
  • Develop
  • Release notes
  • Lines, bars and markers
  • Images, contours and fields
  • Subplots, axes and figures
  • Statistics
  • Pie and polar charts
  • Text, labels and annotations
  • Color
  • Shapes and collections
  • Style sheets
  • Module - pyplot
  • Module - axes_grid1
  • Module - axisartist
  • Showcase
  • Animation
  • Event handling
  • Miscellaneous
  • 3D plotting
    • Plot 2D data on 3D plot
    • Demo of 3D bar charts
    • Create 2D bar graphs in different planes
    • 3D box surface plot
    • Plot contour (level) curves in 3D
    • Plot contour (level) curves in 3D using the extend3d option
    • Project contour profiles onto a graph
    • Filled contours
    • Project filled contour onto a graph
    • Custom hillshading in a 3D surface plot
    • 3D errorbars
    • Create 3D histogram of 2D data
    • Parametric curve
    • Lorenz attractor
    • 2D and 3D axes in same figure
    • Automatic text offsetting
    • Draw flat objects in 3D plot
    • Generate polygons to fill under 3D line graph
    • 3D plot projection types
    • 3D quiver plot
    • Rotating a 3D plot
    • 3D scatterplot
    • 3D stem
    • 3D plots as subplots
    • 3D surface (colormap)
    • 3D surface (solid color)
    • 3D surface (checkerboard)
    • 3D surface with polar coordinates
    • Text annotations in 3D
    • Triangular 3D contour plot
    • Triangular 3D filled contour plot
    • Triangular 3D surfaces
    • More triangular 3D surfaces
    • Primary 3D view planes
    • 3D voxel / volumetric plot
    • 3D voxel plot of the NumPy logo
    • 3D voxel / volumetric plot with RGB colors
    • 3D voxel / volumetric plot with cylindrical coordinates
    • 3D wireframe plot
    • Animate a 3D wireframe plot
    • 3D wireframe plots in one direction
  • Scales
  • Specialty plots
  • Spines
  • Ticks
  • Units
  • Embedding Matplotlib in graphical user interfaces
  • Widgets
  • Userdemo

Note

Go to the end to download the full example code

3D voxel plot of the NumPy logo¶

Demonstrates using Axes3D.voxels with uneven coordinates.

import matplotlib.pyplot as plt
import numpy as np


def explode(data):
    size = np.array(data.shape)*2
    data_e = np.zeros(size - 1, dtype=data.dtype)
    data_e[::2, ::2, ::2] = data
    return data_e

# build up the numpy logo
n_voxels = np.zeros((4, 3, 4), dtype=bool)
n_voxels[0, 0, :] = True
n_voxels[-1, 0, :] = True
n_voxels[1, 0, 2] = True
n_voxels[2, 0, 1] = True
facecolors = np.where(n_voxels, '#FFD65DC0', '#7A88CCC0')
edgecolors = np.where(n_voxels, '#BFAB6E', '#7D84A6')
filled = np.ones(n_voxels.shape)

# upscale the above voxel image, leaving gaps
filled_2 = explode(filled)
fcolors_2 = explode(facecolors)
ecolors_2 = explode(edgecolors)

# Shrink the gaps
x, y, z = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2
x[0::2, :, :] += 0.05
y[:, 0::2, :] += 0.05
z[:, :, 0::2] += 0.05
x[1::2, :, :] += 0.95
y[:, 1::2, :] += 0.95
z[:, :, 1::2] += 0.95

ax = plt.figure().add_subplot(projection='3d')
ax.voxels(x, y, z, filled_2, facecolors=fcolors_2, edgecolors=ecolors_2)
ax.set_aspect('equal')

plt.show()

Download Jupyter notebook: voxels_numpy_logo.ipynb

Download Python source code: voxels_numpy_logo.py

Gallery generated by Sphinx-Gallery

© Copyright 2002–2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team; 2012–2024 The Matplotlib development team.

Created using Sphinx 7.3.7.