Draw a curve connecting two points instead of a straight line in matplotlib

In matplotlib, you can create smooth curves between two points using mathematical functions instead of straight lines. This technique is useful for creating aesthetically pleasing connections or modeling natural phenomena.

Basic Curve Drawing Method

We'll create a function that generates a hyperbolic cosine curve between two points ?

import matplotlib.pyplot as plt
import numpy as np

def draw_curve(p1, p2):
    # Calculate curve parameters using hyperbolic cosine
    a = (p2[1] - p1[1]) / (np.cosh(p2[0]) - np.cosh(p1[0]))
    b = p1[1] - a * np.cosh(p1[0])
    
    # Generate smooth curve points
    x = np.linspace(p1[0], p2[0], 100)
    y = a * np.cosh(x) + b
    
    return x, y

# Define two points
p1 = [0, 1]
p2 = [1, 2]

# Generate curve coordinates
x, y = draw_curve(p1, p2)

# Plot the points and curve
plt.figure(figsize=(8, 5))
plt.plot(p1[0], p1[1], 'ro', markersize=8, label='Point 1')
plt.plot(p2[0], p2[1], 'bo', markersize=8, label='Point 2')
plt.plot(x, y, 'g-', linewidth=2, label='Curve')
plt.grid(True, alpha=0.3)
plt.legend()
plt.title('Curve Connecting Two Points')
plt.show()

Alternative Curve Types

Quadratic Curve

Create a parabolic curve using a quadratic function ?

import matplotlib.pyplot as plt
import numpy as np

def quadratic_curve(p1, p2, curve_height=0.5):
    # Mid-point with added height for curve
    mid_x = (p1[0] + p2[0]) / 2
    mid_y = (p1[1] + p2[1]) / 2 + curve_height
    
    # Generate points along quadratic curve
    t = np.linspace(0, 1, 100)
    x = (1-t)**2 * p1[0] + 2*(1-t)*t * mid_x + t**2 * p2[0]
    y = (1-t)**2 * p1[1] + 2*(1-t)*t * mid_y + t**2 * p2[1]
    
    return x, y

# Define points
p1 = [0, 1]
p2 = [3, 1]

# Generate different curve heights
fig, axes = plt.subplots(1, 3, figsize=(12, 4))

for i, height in enumerate([0.5, 1.0, 1.5]):
    x, y = quadratic_curve(p1, p2, height)
    axes[i].plot(p1[0], p1[1], 'ro', markersize=8)
    axes[i].plot(p2[0], p2[1], 'bo', markersize=8)
    axes[i].plot(x, y, 'g-', linewidth=2)
    axes[i].set_title(f'Height: {height}')
    axes[i].grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

Sine Wave Curve

Create a sinusoidal curve between two points ?

import matplotlib.pyplot as plt
import numpy as np

def sine_curve(p1, p2, frequency=1, amplitude=0.5):
    # Linear interpolation with sine wave added
    t = np.linspace(0, 1, 100)
    x = p1[0] + t * (p2[0] - p1[0])
    y = p1[1] + t * (p2[1] - p1[1]) + amplitude * np.sin(frequency * np.pi * t)
    
    return x, y

# Define points
p1 = [0, 0]
p2 = [4, 2]

# Plot different frequencies
plt.figure(figsize=(10, 6))

for freq in [1, 2, 3]:
    x, y = sine_curve(p1, p2, frequency=freq, amplitude=0.3)
    plt.plot(x, y, linewidth=2, label=f'Frequency: {freq}')

plt.plot(p1[0], p1[1], 'ro', markersize=10, label='Start Point')
plt.plot(p2[0], p2[1], 'bo', markersize=10, label='End Point')
plt.grid(True, alpha=0.3)
plt.legend()
plt.title('Sine Wave Curves Between Two Points')
plt.show()

Comparison of Curve Types

Curve Type Mathematical Function Best For Parameters
Hyperbolic Cosine cosh(x) Natural hanging curves Automatic fitting
Quadratic Bézier curve Smooth arcs Curve height
Sine Wave sin(x) Oscillating paths Frequency, amplitude

Conclusion

Choose hyperbolic cosine curves for natural catenary shapes, quadratic curves for smooth arcs, and sine waves for oscillating connections. Each method offers different aesthetic and mathematical properties for connecting points with curves instead of straight lines.

Updated on: 2026-03-26T13:20:15+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements