Table of Contents
In Pandas, you can add titles to plots by using the set_title() method. This method takes one parameter, which is a string containing the title for the plot. The title will then be displayed above the plot. You can also use the title() and suptitle() methods to add titles to specific axes and to the entire plot, respectively.
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.