When I first started working with Python Matplotlib, I was fascinated by how easily it could visualize complex datasets in 2D. But when I discovered the power of 3D plots, it opened a whole new world of possibilities.
In this tutorial, I’ll show you how to create a 3D scatter plot with a line and a surface in Python using Matplotlib. I’ll walk you through two methods for each, one using simple built-in functions and another using more advanced customization.
If you’re someone who wants to visualize 3D data effectively, this guide will help you master the process step-by-step.
What is a 3D Scatter Plot in Python Matplotlib?
A 3D scatter plot is a type of plot that displays points in three-dimensional space. Each point has three coordinates, X, Y, and Z, which makes it ideal for representing relationships between three numerical variables.
In Python Matplotlib, we can use the Axes3D module from mpl_toolkits.mplot3d to create 3D plots easily. Once you understand the basics, you can add lines or even overlay surfaces to make your visualization more meaningful.
3D Scatter Plot with Line in Python Matplotlib
When I visualize 3D data, I often want to connect the points to show a trend or path. Matplotlib allows us to easily add a 3D line to a scatter plot. Let’s look at two different methods to do this.
Method 1 – Use Axes3D to Plot 3D Scatter with Line
This is the simplest and most direct way to create a 3D scatter plot with a line in Python. We’ll use the scatter() method for the points and plot() for the connecting line.
# Import necessary libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Generate sample data
np.random.seed(42)
x = np.linspace(0, 10, 50)
y = np.sin(x)
z = np.cos(x)
# Create figure and 3D axis
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Plot 3D scatter
ax.scatter(x, y, z, color='red', s=50, label='Data Points')
# Plot connecting line
ax.plot(x, y, z, color='blue', linewidth=2, label='Connecting Line')
# Add labels and title
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('3D Scatter Plot with Line in Python Matplotlib')
ax.legend()
# Show plot
plt.show()You can see the output in the screenshot below.

This method is great for beginners who want to quickly visualize 3D data. The line helps show the direction or trend of the data points.
You can modify the color, marker size, and line style to make your visualization more appealing.
Method 2 – Use Matplotlib’s Line3DCollection for Multiple Lines
Sometimes, you may want to draw multiple lines or segments between points. In such cases, Line3DCollection from Matplotlib’s 3D toolkit is more flexible.
Here’s how I do it when working with segmented data.
from mpl_toolkits.mplot3d.art3d import Line3DCollection
# Generate data
x = np.linspace(0, 10, 10)
y = np.sin(x)
z = np.cos(x)
# Create figure
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Scatter plot
ax.scatter(x, y, z, color='green', s=60)
# Create line segments
points = np.array([x, y, z]).T.reshape(-1, 1, 3)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
# Add lines
lc = Line3DCollection(segments, colors='orange', linewidths=2)
ax.add_collection(lc)
# Labels and title
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('3D Scatter Plot with Multiple Line Segments in Python')
plt.show()You can see the output in the screenshot below.

This method gives you more control over the lines. You can color them individually or apply gradients to represent variable intensity. It’s especially useful when visualizing trajectories or movement paths in 3D space.
3D Scatter Plot with Surface in Python Matplotlib
Adding a surface to a 3D scatter plot can help you visualize the relationship between scattered data points and a continuous function or trend.
I often use this approach to compare real data (scatter points) with a theoretical model (surface). Let’s explore two practical methods to create a scatter plot with a surface in Python.
Method 1 – Use plot_surface() Function
This is the most common way to add a surface to your 3D scatter plot. The plot_surface() function creates a smooth surface over a grid of X, Y, and Z values.
Here’s the complete example:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Generate grid data
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
# Generate random scatter points
x_scatter = np.random.uniform(-5, 5, 50)
y_scatter = np.random.uniform(-5, 5, 50)
z_scatter = np.sin(np.sqrt(x_scatter**2 + y_scatter**2)) + np.random.normal(0, 0.1, 50)
# Create 3D plot
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Plot surface
ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.6)
# Add scatter points
ax.scatter(x_scatter, y_scatter, z_scatter, color='red', s=50, label='Data Points')
# Labels and title
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('3D Scatter Plot with Surface in Python Matplotlib')
ax.legend()
plt.show()You can see the output in the screenshot below.

This method works great when you want to visualize how your data points align with a mathematical surface.
The alpha parameter controls transparency, allowing you to see both the scatter points and the surface clearly.
Method 2 – Use plot_trisurf() for Irregular Data
If your data points are irregularly spaced, plot_trisurf() is a better choice. It creates a surface by triangulating the data points, making it ideal for real-world datasets.
# Import libraries
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Generate irregular data
x = np.random.uniform(-5, 5, 100)
y = np.random.uniform(-5, 5, 100)
z = np.sin(np.sqrt(x**2 + y**2))
# Create 3D plot
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Plot scatter
ax.scatter(x, y, z, color='red', s=40, label='Data Points')
# Plot surface using triangulation
ax.plot_trisurf(x, y, z, cmap='plasma', alpha=0.7)
# Labels and title
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('3D Scatter Plot with Triangular Surface in Python')
ax.legend()
plt.show()You can see the output in the screenshot below.

This method is particularly useful for datasets collected from sensors or geographical sources where the data isn’t evenly spaced. It’s one of my favorite techniques for visualizing real-world 3D data in Python.
Customize the 3D Scatter Plot in Python Matplotlib
Once you’ve created your 3D scatter plot with line and surface, you can enhance it by adding customizations such as color maps, markers, and lighting effects.
Here are a few quick customization tips:
- Use cmap to apply color gradients to your surface.
- Adjust alpha for better transparency and visibility.
- Use ax.view_init(elev, azim) to change the viewing angle.
- Add a color bar using plt.colorbar() for better interpretation.
These small tweaks can make your Python Matplotlib visualizations look more professional and informative.
When to Use 3D Scatter Plots with Line and Surface
In my experience, 3D scatter plots with lines and surfaces are best used when you want to:
- Visualize multi-dimensional relationships in data.
- Compare observed data points with a model surface.
- Represent movement or trajectory in 3D space.
- Present complex data in a visually intuitive manner.
For example, if you’re analyzing sales performance across U.S. states over time, a 3D scatter plot with a connecting line can show trends, while a surface can represent predicted performance.
Conclusion
Creating 3D scatter plots with line and surface in Python Matplotlib is a powerful way to visualize data in three dimensions.
Whether you’re working on scientific research, data analytics, or business visualization, these techniques can help you uncover insights that 2D charts can’t show.
I’ve personally used these methods in multiple projects, from visualizing sensor data to modeling financial trends, and they never fail to impress.
Experiment with different color maps, angles, and data sources to make your plots more engaging and insightful.
You may read:
- Create a Horizontal Stacked Bar Chart in Matplotlib
- Create 3D Scatter Plot with Color in Python Matplotlib
- Change Marker Size in 3D Scatter Plot using Matplotlib
- How to Rotate a 3D Scatter Plot in Python Matplotlib

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.