How to curve text in a polar plot in matplotlib?

Creating curved text in a polar plot requires plotting lines along curved paths and positioning text elements along angular coordinates. This technique is useful for creating circular labels, curved annotations, and artistic text layouts in matplotlib polar plots.

Basic Setup

First, let's create a basic polar plot with curved lines ?

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d

plt.rcParams["figure.figsize"] = [8, 6]
plt.rcParams["figure.autolayout"] = True

fig = plt.figure()
ax = fig.add_subplot(111, projection="polar")

# Create radial lines at specific angles
for degree in [0, 90, 180, 270]:
    rad = np.deg2rad(degree)
    ax.plot([rad, rad], [0, 1], color="lightgray", linewidth=1, alpha=0.7)

plt.show()

Adding Curved Lines

Now let's add curved lines that will serve as paths for our text ?

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection="polar")

# Define curved paths
curves = [
    [[0, 90], [0.3, 0.7]],    # First curve from 0° to 90°
    [[120, 240], [0.5, 0.8]], # Second curve from 120° to 240°
    [[270, 360], [0.4, 0.6]]  # Third curve from 270° to 360°
]

for curve in curves:
    # Convert degrees to radians
    angles_deg = curve[0]
    radii = curve[1]
    angles_rad = np.deg2rad(angles_deg)
    
    # Create smooth interpolation
    theta = np.linspace(angles_rad[0], angles_rad[1], 100)
    r = interp1d(angles_rad, radii)(theta)
    
    # Plot the curved line
    ax.plot(theta, r, linewidth=3, color='red', alpha=0.8)

ax.set_ylim(0, 1)
plt.show()

Adding Text Along Curves

To add text that follows the curve, we position text elements at calculated points along the path ?

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d

fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection="polar")

# Define a curved path
curve_angles = [0, 90]
curve_radii = [0.4, 0.8]
angles_rad = np.deg2rad(curve_angles)

# Create smooth curve
theta_smooth = np.linspace(angles_rad[0], angles_rad[1], 100)
r_smooth = interp1d(angles_rad, curve_radii)(theta_smooth)

# Plot the curve
ax.plot(theta_smooth, r_smooth, linewidth=4, color='blue', alpha=0.7, label='Text Path')

# Add text along the curve
text = "CURVED TEXT EXAMPLE"
n_chars = len(text)

# Calculate positions for each character
for i, char in enumerate(text):
    # Position along the curve (0 to 1)
    position = i / (n_chars - 1)
    
    # Interpolate angle and radius
    char_theta = angles_rad[0] + position * (angles_rad[1] - angles_rad[0])
    char_r = curve_radii[0] + position * (curve_radii[1] - curve_radii[0])
    
    # Calculate rotation angle for the character
    rotation = np.rad2deg(char_theta) - 90
    
    # Add the character
    ax.text(char_theta, char_r, char, 
           rotation=rotation, 
           ha='center', va='center',
           fontsize=12, fontweight='bold', color='darkred')

ax.set_ylim(0, 1)
ax.set_title("Curved Text in Polar Plot", fontsize=16, pad=20)
ax.legend(loc='upper right')
plt.show()

Advanced Curved Text Example

Here's a more sophisticated example with multiple curved text labels ?

import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d

fig = plt.figure(figsize=(12, 10))
ax = fig.add_subplot(111, projection="polar")

# Define multiple curved text paths
text_configs = [
    {"text": "OUTER CIRCLE", "angles": [0, 180], "radii": [0.8, 0.8], "color": "blue"},
    {"text": "INNER ARC", "angles": [200, 340], "radii": [0.5, 0.5], "color": "green"},
    {"text": "SPIRAL TEXT", "angles": [0, 270], "radii": [0.2, 0.7], "color": "red"}
]

for config in text_configs:
    angles_rad = np.deg2rad(config["angles"])
    text = config["text"]
    n_chars = len(text)
    
    # Create path
    theta_path = np.linspace(angles_rad[0], angles_rad[1], 100)
    r_path = interp1d(angles_rad, config["radii"])(theta_path)
    
    # Plot the path
    ax.plot(theta_path, r_path, '--', alpha=0.3, color=config["color"])
    
    # Add characters along the path
    for i, char in enumerate(text):
        if char == ' ':
            continue
            
        position = i / (n_chars - 1)
        char_theta = angles_rad[0] + position * (angles_rad[1] - angles_rad[0])
        char_r = config["radii"][0] + position * (config["radii"][1] - config["radii"][0])
        
        # Adjust rotation based on position
        rotation = np.rad2deg(char_theta)
        if char_theta > np.pi/2 and char_theta < 3*np.pi/2:
            rotation += 180
        
        ax.text(char_theta, char_r, char,
               rotation=rotation - 90,
               ha='center', va='center',
               fontsize=14, fontweight='bold',
               color=config["color"])

# Customize the plot
ax.set_ylim(0, 1)
ax.set_title("Multiple Curved Text Labels in Polar Plot", fontsize=18, pad=30)
ax.grid(True, alpha=0.3)
plt.show()

Key Points

  • Text Rotation: Calculate rotation angles based on the curve's tangent direction
  • Character Spacing: Distribute characters evenly along the curve path
  • Interpolation: Use scipy.interpolate.interp1d for smooth curves
  • Positioning: Convert between polar coordinates (theta, r) for proper text placement

Conclusion

Curving text in polar plots involves creating curved paths and positioning individual characters along these paths with appropriate rotations. Use interpolation for smooth curves and calculate rotation angles to make text follow the curve naturally.

---
Updated on: 2026-03-26T15:23:20+05:30

932 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements