2.4 C
London
Friday, December 20, 2024
HomePandas in PythonGeneral Functions in PythonHow to Add Titles to Plots in Pandas (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories