How to add a title to a plot

1 min

Here is a simple example of a line plot, using the matplotlib library.

import matplotlib.pyplot as plt
import pandas as pd

# We create our dataframe
df = pd.DataFrame(index=range(0,10), data={"col1" : range(0,10)})

fig, axes = plt.subplots(1,1, figsize=(8,6))

# We do a line plot on the axes
axes.plot(df.index, df["col1"])

# Fixing the layout to fit the size
fig.tight_layout()

# Showing the plot
plt.show()
A simple line plot example using Matplotlib

Adding a title

As you can see in the example above we don't have a title on the graph.

We can easily add a title using the axes.set_title() method.

import matplotlib.pyplot as plt
import pandas as pd

# We create our dataframe
df = pd.DataFrame(index=range(0,10), data={"col1" : range(0,10)})

fig, axes = plt.subplots(1,1, figsize=(8,6))

# We do a line plot on the axes
axes.plot(df.index, df["col1"])

# Adding the title to the plot
axes.set_title("My amazing plot")

# Fixing the layout to fit the size
fig.tight_layout()

# Showing the plot
plt.show()
Adding a title to the plot

See here we use the axes object to call the set_title() method.

Here you are ! You know know how to add a title to any plot using Matplotlib.

More on plots

If you want to know more about how to add labels, plot different types of plots, etc... checkout the other articles I wrote on the topic, just here :

Matplotlib - The Python You Need
We gathered the only Python essentials that you will probably ever need.