Annotations
To give further explanation or guide readers to focus on certain remarkable details, annotations can be added to a figure. Matplotlib offers several modules to add text, arrows, and shapes that can be exploited.
Adding text annotations
Annotations can be added easily by specifying the desired locations through some built-in functions in Matplotlib.
Adding text and arrows with axis.annotate
Matplotlib has an axis.annotate
function that draws an arrow extending across specified x and y coordinates and adds a text label if a string is input. The target coordinates of the pointed location and text label are assigned by the xy
and xytext
parameters in tuples, respectively. Here is an example of drawing the basic demand-supply curves we learn in high school economics:
import matplotlib.pyplot as plt import numpy as np # create 1000 equally spaced points between -10 and 10 x = np.linspace(0, 10) # Prepare the data y1 = x y2 = 10-x # Plot the data fig, ax = plt.subplots() plt.plot(x,y1...