Matplotlib: How to add border line around text box

In order to add a red border around a box in matplotlib, use

bbox=dict(..., edgecolor='red', linewidth=4,)

Full example


# Import matplotlib's pyplot
import matplotlib.pyplot as plt
import numpy as np"ggplot")

# Generate a range of x values
x = np.linspace(0, 2 * np.pi, 100)

# Calculate the sine of x values
y = np.sin(x)

# Create the plot
plt.plot(x, y)

# Add a textbox to the plot
plt.text(4, 0.8, 'y = sin(x)', fontsize=12, bbox=dict(
    facecolor='gray', alpha=1.0,
    edgecolor='red', linewidth=4,

# Display the plot