Fundamentals 8 min read

Master 3D Plotting in Python: Matplotlib & Plotly Step‑by‑Step

This guide provides ready‑to‑run Python scripts for creating 3D scatter, line, surface, bar, and contour charts with Matplotlib, plus interactive 3D visualizations using Plotly, and includes pip installation commands for all required libraries.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Master 3D Plotting in Python: Matplotlib & Plotly Step‑by‑Step

The article presents a collection of Python scripts that demonstrate how to generate a variety of 3D visualizations using both Matplotlib and Plotly. Each section includes the full code, a brief description, and an example image of the resulting chart, followed by installation instructions for the required packages.

Matplotlib 3D Scatter Plot

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def plot_3d_scatter():
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    x = np.random.standard_normal(100)
    y = np.random.standard_normal(100)
    z = np.random.standard_normal(100)
    ax.scatter(x, y, z)
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    plt.show()

plot_3d_scatter()

Matplotlib 3D Line Plot

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def plot_3d_line():
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    t = np.linspace(0, 10, 100)
    x = np.sin(t)
    y = np.cos(t)
    z = t
    ax.plot(x, y, z)
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    plt.show()

plot_3d_line()

Matplotlib 3D Surface Plot

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def plot_3d_surface():
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    X = np.arange(-5, 5, 0.25)
    Y = np.arange(-5, 5, 0.25)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    surf = ax.plot_surface(X, Y, Z, cmap='viridis')
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    fig.colorbar(surf, shrink=0.5, aspect=5)
    plt.show()

plot_3d_surface()

Matplotlib 3D Bar Plot

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def plot_3d_bar():
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    _x = np.array([1,2,3,2,1,2,3,2,1])
    _y = np.array([1,2,3,2,3,4,5,4,5])
    _z = np.array([1,2,3,4,5,6,7,8,9])
    dx = dy = 0.8 * np.ones_like(_z)
    dz = _z
    ax.bar3d(_x, _y, dz*0, dx, dy, dz, color='b')
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    plt.show()

plot_3d_bar()

Matplotlib 3D Contour Plot

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def plot_3d_contour():
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    X = np.arange(-5, 5, 0.25)
    Y = np.arange(-5, 5, 0.25)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    ax.contour3D(X, Y, Z, 50, cmap='binary')
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    ax.view_init(60, 35)
    plt.show()

plot_3d_contour()

Plotly Interactive 3D Scatter Plot

import plotly.graph_objects as go
import numpy as np

def plot_interactive_3d_scatter():
    x = np.random.standard_normal(100)
    y = np.random.standard_normal(100)
    z = np.random.standard_normal(100)
    fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z,
                                       mode='markers',
                                       marker=dict(size=6, color=z, colorscale='Viridis', opacity=0.8))])
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
    fig.show()

plot_interactive_3d_scatter()

Plotly Interactive 3D Line Plot

import plotly.graph_objects as go
import numpy as np

def plot_interactive_3d_line():
    t = np.linspace(0, 10, 100)
    x = np.sin(t)
    y = np.cos(t)
    z = t
    fig = go.Figure(data=go.Scatter3d(x=x, y=y, z=z, mode='lines'))
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
    fig.show()

plot_interactive_3d_line()

Plotly Interactive 3D Surface Plot

import plotly.graph_objects as go
import numpy as np

def plot_interactive_3d_surface():
    x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
    y = x.copy().T
    z = np.cos(x ** 2 + y ** 2)
    fig = go.Figure(data=[go.Surface(x=x, y=y, z=z)])
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
    fig.show()

plot_interactive_3d_surface()

Installation Commands

To run the examples, install the required libraries with pip:

pip install matplotlib numpy scikit-image
pip install mayavi
pip install plotly
pip install pyvista

Choose the library that best fits your visualization needs.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonTutorialData visualizationMatplotlib3d-visualizationplotly
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.