3D Lines and Scatter Plots
Learn how to create three-dimensional line plots and scatter plots using Matplotlib. Explore coordinate data, multiple data series, markers, colors and plot customization.
1. Learning Objectives
After completing this lesson, you will understand how to:
- Create 3D line plots using Matplotlib.
- Create 3D scatter plots using X, Y and Z coordinates.
- Customize line colors, widths and marker styles.
- Display multiple 3D data series in one plot.
- Use NumPy arrays to generate coordinate data.
- Add titles, labels, legends and viewing angles.
2. Introduction to 3D Line and Scatter Plots
Matplotlib provides methods to visualize data in three-dimensional space. A 3D line plot connects coordinate points, while a 3D scatter plot displays individual points without connecting them.
Both plot types use three coordinate values: X, Y and Z. The plotting methods are available through a 3D Axes object.
3D Line Plot vs 3D Scatter Plot
| Feature | 3D Line Plot | 3D Scatter Plot |
|---|---|---|
| Method | ax.plot() |
ax.scatter() |
| Representation | Connects data points using lines. | Displays individual data points. |
| Useful for | Paths, trends and continuous sequences. | Distribution and relationships between values. |
| Customization | Line style, color and width. | Marker size, shape and color. |
3. Create a Basic 3D Line Plot
The ax.plot() method creates a 3D line plot when
it is called on a 3D Axes object with X, Y and Z coordinate values.
Basic Syntax
ax.plot(
x,
y,
z
)
Example: Simple 3D Line
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 5, 6]
z = [1, 3, 2, 4, 5]
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(
111,
projection="3d"
)
ax.plot(
x,
y,
z
)
ax.set_title("Basic 3D Line Plot")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
plt.tight_layout()
plt.show()
Output
Figure: Basic 3D Line Plot
A 3D line connects the following coordinates in order:
(1, 2, 1)
(2, 4, 3)
(3, 3, 2)
(4, 5, 4)
(5, 6, 5)
The chart displays X, Y and Z coordinate axes.
4. Customize a 3D Line Plot
You can customize a 3D line using color, line width and line style. These properties help make different data series easier to identify.
Example: Line Color and Width
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 5, 6]
z = [1, 3, 2, 4, 5]
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(
111,
projection="3d"
)
ax.plot(
x,
y,
z,
color="blue",
linewidth=3,
linestyle="-"
)
ax.set_title("Customized 3D Line Plot")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
plt.show()
Output
Figure: Customized 3D Line Plot
A blue 3D line is displayed.
Line width: 3
Line style: Solid
The line connects five coordinate points.
Common Line Style Options
| Style | Value | Description |
|---|---|---|
| Solid | "-" |
Continuous line. |
| Dashed | "--" |
Dashed line. |
| Dotted | ":" |
Dotted line. |
| Dash-dot | "-." |
Dash and dot pattern. |
5. Add Markers to a 3D Line Plot
Markers highlight the individual coordinate points along a line.
Use the marker parameter to select a marker shape.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 5, 6]
z = [1, 3, 2, 4, 5]
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(
111,
projection="3d"
)
ax.plot(
x,
y,
z,
color="green",
linewidth=2,
marker="o",
markersize=8,
label="Data Path"
)
ax.set_title("3D Line Plot with Markers")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
ax.legend()
plt.show()
Output
Figure: 3D Line Plot with Markers
A green 3D line connects five coordinate points.
Circular markers highlight each data point.
Legend: Data Path
6. Create a Basic 3D Scatter Plot
The ax.scatter() method displays individual points
in three-dimensional space. Each point is positioned using its
X, Y and Z coordinate values.
Basic Syntax
ax.scatter(
x,
y,
z
)
Example: Simple 3D Scatter Plot
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [5, 3, 4, 2, 6]
z = [2, 4, 1, 5, 3]
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(
111,
projection="3d"
)
ax.scatter(
x,
y,
z
)
ax.set_title("Basic 3D Scatter Plot")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
plt.tight_layout()
plt.show()
Output
Figure: Basic 3D Scatter Plot
Five individual points are displayed:
(1, 5, 2)
(2, 3, 4)
(3, 4, 1)
(4, 2, 5)
(5, 6, 3)
The points are not connected by lines.
7. Customize 3D Scatter Points
Scatter plots support different marker sizes, shapes and colors. You can use these properties to highlight important data points.
Example: Marker Size and Color
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [5, 3, 4, 2, 6]
z = [2, 4, 1, 5, 3]
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(
111,
projection="3d"
)
ax.scatter(
x,
y,
z,
color="red",
s=100,
marker="o",
alpha=0.8
)
ax.set_title("Customized 3D Scatter Plot")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
plt.show()
Output
Figure: Customized 3D Scatter Plot
Five red circular markers are displayed.
Marker size: 100
Transparency: 0.8
Each point represents one X, Y and Z coordinate.
Common Scatter Marker Options
| Marker | Value | Shape |
|---|---|---|
| Circle | "o" |
Circular marker. |
| Square | "s" |
Square marker. |
| Triangle | "^" |
Upward triangle. |
| Diamond | "D" |
Diamond marker. |
| Cross | "x" |
Cross marker. |
8. Use Colors to Represent Data Values
You can use the c parameter to assign colors based
on numerical values. The cmap parameter selects the
colormap used to map values to colors.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [5, 3, 4, 2, 6]
z = [2, 4, 1, 5, 3]
values = [10, 20, 30, 40, 50]
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(
111,
projection="3d"
)
scatter = ax.scatter(
x,
y,
z,
c=values,
cmap="viridis",
s=100
)
fig.colorbar(
scatter,
ax=ax,
label="Data Value",
pad=0.1
)
ax.set_title("3D Scatter with Color Mapping")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
plt.show()
Output
Figure: 3D Scatter with Color Mapping
Five scatter points are displayed.
Point colors represent the values 10, 20, 30, 40 and 50.
Colormap: viridis
A colorbar indicates the numerical value mapping.
9. Plot Multiple 3D Lines
You can draw multiple 3D lines in the same Axes object by calling
ax.plot() multiple times. Use different colors and
labels to distinguish the data series.
Example: Two 3D Lines
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 3, 5, 6]
z1 = [1, 3, 2, 4, 5]
y2 = [5, 3, 4, 2, 3]
z2 = [4, 2, 5, 3, 6]
fig = plt.figure(figsize=(9, 6))
ax = fig.add_subplot(
111,
projection="3d"
)
ax.plot(
x,
y1,
z1,
color="blue",
linewidth=2,
label="Series One"
)
ax.plot(
x,
y2,
z2,
color="red",
linewidth=2,
label="Series Two"
)
ax.set_title("Multiple 3D Line Plots")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
ax.legend()
plt.show()
Output
Figure: Multiple 3D Line Plots
Blue line: Series One
Red line: Series Two
Both lines are displayed in the same 3D plotting area.
A legend identifies the two data series.
10. Combine 3D Lines and Scatter Points
You can combine line and scatter plots in one 3D Axes object. This is useful when you want to show a connected path and highlight the individual coordinate points.
Example: Line with Scatter Points
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 5, 6]
z = [1, 3, 2, 4, 5]
fig = plt.figure(figsize=(9, 6))
ax = fig.add_subplot(
111,
projection="3d"
)
ax.plot(
x,
y,
z,
color="blue",
linewidth=2,
label="Connected Path"
)
ax.scatter(
x,
y,
z,
color="red",
s=80,
label="Data Points"
)
ax.set_title("3D Line and Scatter Combination")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
ax.legend()
plt.show()
Output
Figure: 3D Line and Scatter Combination
A blue line connects five coordinate points.
Red markers highlight the individual points.
Legend:
- Connected Path
- Data Points
11. Generate 3D Coordinates Using NumPy
NumPy makes it easier to generate numerical sequences for 3D plots.
Functions such as linspace() and mathematical
operations can be used to create coordinate values.
Example: 3D Parametric Line
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 10, 100)
x = np.cos(t)
y = np.sin(t)
z = t
fig = plt.figure(figsize=(9, 6))
ax = fig.add_subplot(
111,
projection="3d"
)
ax.plot(
x,
y,
z,
color="purple",
linewidth=2
)
ax.set_title("3D Parametric Line")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
plt.show()
Output
Figure: 3D Parametric Line
A spiral-shaped 3D line is displayed.
The line contains 100 coordinate samples.
X = cos(t)
Y = sin(t)
Z = t
The line moves upward as t increases.
12. Customize the 3D Viewing Angle
The viewing angle controls how you observe a 3D plot.
Matplotlib provides the view_init() method to
customize the elevation and azimuth angles.
ax.view_init(
elev=30,
azim=45
)
| Parameter | Description |
|---|---|
elev |
Controls the elevation angle of the camera. |
azim |
Controls the azimuth angle around the vertical axis. |
Complete Example
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 5, 6]
z = [1, 3, 2, 4, 5]
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(
111,
projection="3d"
)
ax.plot(
x,
y,
z,
marker="o",
color="green"
)
ax.view_init(
elev=35,
azim=60
)
ax.set_title("3D Plot with Custom View")
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
ax.set_zlabel("Z-axis")
plt.show()
Output
Figure: 3D Plot with Custom View
A green 3D line with circular markers is displayed.
Elevation angle: 35 degrees
Azimuth angle: 60 degrees
The camera position changes the visible perspective of the plot.
13. Important 3D Plotting Methods and Parameters
| Method / Parameter | Purpose |
|---|---|
ax.plot() |
Creates a 3D line plot. |
ax.scatter() |
Creates a 3D scatter plot. |
color |
Sets the line or marker color. |
linewidth |
Controls the line width. |
linestyle |
Controls the line pattern. |
marker |
Sets the marker shape. |
markersize |
Controls marker size in line plots. |
s |
Controls marker area in scatter plots. |
cmap |
Selects the colormap for numerical color mapping. |
view_init() |
Changes the camera viewing angle. |
14. Common Mistakes
| Mistake | Explanation | Solution |
|---|---|---|
| Using a 2D Axes | A normal Axes object does not provide 3D plotting methods. | Create the Axes with projection="3d". |
| Missing Z coordinates | 3D plotting requires a third coordinate. | Provide valid Z values. |
| Different array lengths | Coordinate arrays may not represent matching points. | Check the lengths and shapes of X, Y and Z. |
| Using the wrong scatter size parameter | Scatter marker area and line marker size use different parameter names. | Use s for scatter and markersize for line plots. |
| Missing legend labels | Multiple lines can become difficult to identify. | Add labels and call ax.legend(). |
| Incorrect viewing angle | Some parts of the plot may be difficult to see. | Adjust elev and azim. |
15. Best Practices
- Use 3D line plots when the order of points is important.
- Use scatter plots when you want to show individual observations.
- Choose contrasting colors for multiple data series.
- Add legends when displaying more than one series.
- Use meaningful axis labels and chart titles.
- Use NumPy to generate coordinate sequences efficiently.
- Adjust the viewing angle to improve readability.
- Avoid unnecessary 3D effects when a 2D chart explains the data more clearly.
16. Practical Activity
Student Performance 3D Chart
Create a 3D visualization that compares students' study hours, attendance and examination scores.
Requirements:
- Create three lists for study hours, attendance and exam scores.
- Create a 3D Axes object.
- Display the students using a 3D scatter plot.
- Use different marker sizes to highlight selected students.
- Create a 3D line connecting the student data points.
- Add axis labels and a chart title.
- Add a legend to identify the line and scatter points.
- Adjust the camera viewing angle.
17. Practice Questions
- What is a 3D line plot?
- Which Matplotlib method creates a 3D line plot?
- Which method creates a 3D scatter plot?
- What is the difference between
ax.plot()andax.scatter()? - How do you customize the line width?
- What is the purpose of the
markerparameter? - How do you assign colors based on numerical values?
- How can you display multiple 3D lines in one Axes?
- What is the purpose of
view_init()? - How can NumPy help generate 3D coordinate data?
Summary
Matplotlib provides the ax.plot() method for creating
3D line plots and the ax.scatter() method for creating
3D scatter plots. Both methods use X, Y and Z coordinate values.
You can customize 3D lines with colors, widths, styles and markers. Scatter plots support marker sizes, shapes, transparency and numerical color mapping. Multiple data series can also be displayed inside the same 3D Axes object.
NumPy helps generate coordinate data, while Matplotlib methods
such as view_init(), set_xlabel() and
legend() help create clear and informative
three-dimensional visualizations.