We are going to introduce additional features to allow even more plot decorations.
We already saw how to use
xlabel(), ylabel(), and
title() to add text around the figure, but we can do something more, namely, add text inside the figure.
text() function does that—writes a string (text) at an arbitrary position (specified by (
plt.text(x, y, text)
Let's plot the sine function, and add a note that says
sin(0) is equal to
In : import matplotlib.pyplot as plt In : import numpy as np In : x = np.arange(0, 2*np.pi, .01) In : y = np.sin(x) In : plt.plot(x, y); In : plt.text(0.1, -0.04, 'sin(0)=0'); In : plt.show()
The output of this code snippet is shown in ...