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
plt.style.use("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,
boxstyle='round,pad=1',
edgecolor='red', linewidth=4,
))
# Display the plot
plt.gcf().set_size_inches(10,5)
plt.show()