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
Draw a parametrized curve using pyplot.plot() in Matplotlib
A parametrized curve is defined by equations where both x and y coordinates are expressed as functions of a parameter (usually t). Matplotlib's pyplot.plot() can easily visualize these curves by plotting the computed x and y coordinates.
Basic Parametrized Curve
Let's create a simple parametrized curve using trigonometric functions ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Number of sample points
N = 400
# Parameter t from 0 to 2?
t = np.linspace(0, 2 * np.pi, N)
# Parametric equations for a cardioid
r = 0.5 + np.cos(t)
x = r * np.cos(t)
y = r * np.sin(t)
# Create plot
fig, ax = plt.subplots()
ax.plot(x, y, 'b-', linewidth=2)
ax.set_title('Parametrized Curve (Cardioid)')
ax.grid(True)
ax.axis('equal')
plt.show()
The output shows a heart-shaped curve called a cardioid ?
[A plot displaying a cardioid curve - heart-shaped parametric curve]
Different Parametrized Curves
Here are examples of various parametrized curves with different equations ?
import numpy as np
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
fig.suptitle('Different Parametrized Curves')
t = np.linspace(0, 2 * np.pi, 1000)
# Spiral
x1 = t * np.cos(t)
y1 = t * np.sin(t)
axes[0,0].plot(x1, y1, 'r-')
axes[0,0].set_title('Spiral')
axes[0,0].grid(True)
# Lissajous curve
x2 = np.cos(3*t)
y2 = np.sin(4*t)
axes[0,1].plot(x2, y2, 'g-')
axes[0,1].set_title('Lissajous Curve')
axes[0,1].grid(True)
# Rose curve
k = 5
x3 = np.cos(k*t) * np.cos(t)
y3 = np.cos(k*t) * np.sin(t)
axes[1,0].plot(x3, y3, 'm-')
axes[1,0].set_title('Rose Curve')
axes[1,0].grid(True)
# Cycloid
R = 1
x4 = R * (t - np.sin(t))
y4 = R * (1 - np.cos(t))
axes[1,1].plot(x4, y4, 'c-')
axes[1,1].set_title('Cycloid')
axes[1,1].grid(True)
plt.tight_layout()
plt.show()
[A 2x2 subplot showing four different parametric curves: spiral, Lissajous curve, rose curve, and cycloid]
Key Steps for Parametrized Curves
| Step | Description | Code Example |
|---|---|---|
| 1 | Define parameter range | t = np.linspace(0, 2*np.pi, N) |
| 2 | Define x(t) equation | x = r * np.cos(t) |
| 3 | Define y(t) equation | y = r * np.sin(t) |
| 4 | Plot the curve | ax.plot(x, y) |
Animating Parametrized Curves
You can create animated parametrized curves by plotting partial parameter ranges ?
import numpy as np
import matplotlib.pyplot as plt
# Create a figure for animation-like effect
fig, ax = plt.subplots(figsize=(8, 6))
# Full parameter range
t_full = np.linspace(0, 4 * np.pi, 1000)
# Plot curve in segments with different colors
segments = 5
colors = ['red', 'blue', 'green', 'orange', 'purple']
for i in range(segments):
start = i * len(t_full) // segments
end = (i + 1) * len(t_full) // segments
t_segment = t_full[start:end]
# Parametric equations for a spiral
x = t_segment * np.cos(t_segment) / 4
y = t_segment * np.sin(t_segment) / 4
ax.plot(x, y, color=colors[i], linewidth=2, alpha=0.8)
ax.set_title('Multi-colored Parametric Spiral')
ax.grid(True)
ax.axis('equal')
plt.show()
[A colorful spiral curve with different colored segments]
Conclusion
Parametrized curves allow you to create complex mathematical shapes by defining x and y as functions of a parameter t. Use np.linspace() to create the parameter range and plt.plot() to visualize the resulting curve.
