In the previous part of the Matplotlib series we were talking about axis ranges. In this part we’ll see how to add a title and axis labels for each axis so that we actually know what it represents.
The method used to set the title is set_title. Let’s use the example from the previous part, this time adding the title:
In [1]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 100)
y = np.sin(x) / x
fig, ax = plt.subplots()
ax.plot(x, y)
# Here's the title.
ax.set_title('Graph Example')
Out[1]:
And now let’s add the axis labels using the set_xlabel and set_ylabel methods for the X and Y axes respectively:
In [2]:
fig, ax = plt.subplots()
ax.plot(x, y)
# title
ax.set_title('Graph Example')
# X label
ax.set_xlabel('x')
# Y label
ax.set_ylabel('y=sin(x)/x')
Out[2]: