How to Add Titles to Plots in Pandas (With Examples)


You can use the title argument to add a title to a plot in pandas:

Method 1: Create One Title

df.plot(kind='hist', title='My Title')

Method 2: Create Multiple Titles for Individual Subplots

df.plot(kind='hist', subplots=True, title=['Title1', 'Title2'])

The following examples show how to use each method with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'points': [10, 10, 12, 12, 15, 17, 20, 20],
                   'assists': [5, 5, 7, 9, 12, 9, 6, 6]})

#view DataFrame
print(df)

  team  points  assists
0    A      10        5
1    A      10        5
2    A      12        7
3    A      12        9
4    B      15       12
5    B      17        9
6    B      20        6
7    B      20        6

Example 1: Create One Title

The following code shows how to add one title to a pandas histogram:

#create histogram with title
df.plot(kind='hist', title='My Title')

Example 2: Create Multiple Titles for Individual Subplots

The following code shows how to create individual titles for subplots in pandas:

df.plot(kind='hist', subplots=True, title=['Title1', 'Title2'])

Notice that each individual subplot has its own title.

Note that you can also pass a list of title names to the title argument:

#define list of subplot titles
title_list = ['Title1', 'Title2']

#pass list of subplot titles to title argument
df.plot(kind='hist', subplots=True, title=title_list)

This plot matches the one we created in the previous example.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Create Pie Chart from Pandas DataFrame
How to Make a Scatterplot From Pandas DataFrame
How to Create a Histogram from Pandas DataFrame

Leave a Reply

Your email address will not be published. Required fields are marked *