Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to plot a 3D continuous line in Matplotlib?
To plot a 3D continuous line in Matplotlib, you need to create three-dimensional data points and use the plot() method with 3D projection. This creates smooth curves in three-dimensional space.
Basic Steps
Follow these steps to create a 3D continuous line plot ?
Import required libraries (NumPy and Matplotlib)
Create x, y, and z data points using NumPy
Create a figure with 3D projection using add_subplot()
Plot the data using plot() method
Display the figure using show() method
Example: 3D Spiral
Here's how to create a 3D spiral line ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [8, 6]
plt.rcParams["figure.autolayout"] = True
# Create parametric data for a 3D spiral
t = np.linspace(0, 4 * np.pi, 100)
x = np.cos(t)
y = np.sin(t)
z = t
# Create 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z, linewidth=2)
# Add labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('3D Spiral Line')
plt.show()
Example: 3D Sine Wave
Creating a more complex 3D continuous line using sine and cosine functions ?
import numpy as np
import matplotlib.pyplot as plt
# Create parametric data
t = np.linspace(0, 6 * np.pi, 200)
x = np.sin(t)
y = np.cos(t)
z = t / 3
# Create 3D plot with custom styling
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z, color='red', linewidth=3, alpha=0.8)
# Customize the plot
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('3D Sine-Cosine Wave')
# Set viewing angle
ax.view_init(elev=20, azim=45)
plt.show()
Customization Options
You can enhance your 3D line plots with various styling options ?
import numpy as np
import matplotlib.pyplot as plt
# Create data
t = np.linspace(0, 4 * np.pi, 150)
x = np.cos(t) * np.exp(-t/10)
y = np.sin(t) * np.exp(-t/10)
z = t
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Plot with custom styling
ax.plot(x, y, z,
color='blue',
linewidth=2.5,
linestyle='-',
marker='o',
markersize=2,
alpha=0.9)
# Customize appearance
ax.set_xlabel('X axis', fontsize=12)
ax.set_ylabel('Y axis', fontsize=12)
ax.set_zlabel('Z axis', fontsize=12)
ax.set_title('3D Decaying Spiral', fontsize=14, fontweight='bold')
# Set background color
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
plt.show()
Key Parameters
| Parameter | Description | Example Values |
|---|---|---|
projection='3d' |
Enables 3D plotting | Required for 3D plots |
linewidth |
Controls line thickness | 1, 2, 3, etc. |
color |
Line color | 'red', 'blue', '#FF5733' |
alpha |
Transparency level | 0.1 to 1.0 |
Conclusion
Use ax.plot(x, y, z) with 3D projection to create continuous lines in three-dimensional space. Combine parametric equations with NumPy to generate smooth curves and customize appearance with color, linewidth, and transparency options.
