How to Add Axis Labels to a Plot in Pandas [5 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. How to Add Axis Labels to a Plot in Pandas
  2. Add Axis Labels to a Plot in Pandas using set_xlabel and set_ylabel
  3. Add Axis Labels to a Plot in Pandas using plt.xlabel() and plt.ylabel()
  4. Setting the x-axis label by setting the DataFrame index name
  5. Add Axis Labels to a Plot in Pandas using axes.set()

# How to Add Axis Labels to a Plot in Pandas

To add axis labels to a Plot in Pandas:

  1. Call the DataFrame.plot() method.
  2. Set the axis labels by supplying the xlabel and ylabel arguments to the method.
main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'team_A': [1, 3, 6, 9, 12, 15], 'team_B': [0, 1, 4, 7, 8, 9], }) df.plot(xlabel='Match', ylabel='Points') plt.show()

add axis labels to plot in pandas

The code for this article is available on GitHub

Make sure you have the pandas and matplotlib modules installed.

shell
pip install pandas matplotlib pip3 install pandas matplotlib

The DataFrame.plot() method makes plots of Series or DataFrame.

We passed the xlabel and ylabel arguments to the method to add axis labels to the plot.

main.py
df.plot(xlabel='Match', ylabel='Points')

The xlabel argument sets the name that should be used for the label on the x-axis.

By default, the method uses the index name as xlabel.

The ylabel argument sets the name that should be used for the label on the y-axis.

By default, no ylabel is shown.

You don't have to supply values for both xlabel and ylabel.

For example, if you only want to show the x-axis label, only pass a value for the xlabel argument and vice versa.

# Add Axis Labels to a Plot in Pandas using set_xlabel and set_ylabel

You can also use the Axes.set_xlabel and Axes.set_ylabel methods to add axis labels to a plot in Pandas.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'team_A': [1, 3, 6, 9, 12, 15], 'team_B': [0, 1, 4, 7, 8, 9], }) axes = df.plot() axes.set_xlabel('Match') axes.set_ylabel('Points') plt.show()

add axis labels to plot using set x label and set y label

The code for this article is available on GitHub

The set_xlabel() method sets the label for the x-axis.

Similarly, the set_ylabel() method sets the label for the y-axis.

# Add Axis Labels to a Plot in Pandas using plt.xlabel() and plt.ylabel()

You can also use the plt.xlabel() and plt.ylabel() methods to achieve the same result.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'team_A': [1, 3, 6, 9, 12, 15], 'team_B': [0, 1, 4, 7, 8, 9], }) df.plot() plt.xlabel('Match') plt.ylabel('Points') plt.show()

add axis labels to plot using plt xlabel and plt ylabel

The code for this article is available on GitHub

The pyplot.xlabel() method sets the label for the x-axis.

The pyplot.ylabel() method sets the label for the y-axis.

Make sure to call the xlabel() and ylabel() methods after you call df.plot(), otherwise, you'd get two plots.

# Setting the x-axis label by setting the DataFrame index name

You can also set the x-axis label by setting the index name of the DataFrame.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'team_A': [1, 3, 6, 9, 12, 15], 'team_B': [0, 1, 4, 7, 8, 9], }) df.index.name = 'Match' df.plot() plt.show()

setting dataframe index name to set x axis label

The code for this article is available on GitHub

By default, the x-axis label uses the index name.

We set the index.name attribute on the DataFrame to "Match", so this is what gets used for the x-axis label.

However, note that the y-axis label is not shown by default, so you would still need to use one of the previous approaches if you also need to show it.

# Add Axis Labels to a Plot in Pandas using axes.set()

You can also use the axes.set() method to add axis labels to a plot in Pandas.

main.py
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'team_A': [1, 3, 6, 9, 12, 15], 'team_B': [0, 1, 4, 7, 8, 9], }) axes = df.plot() axes.set(xlabel='Match', ylabel='Points') plt.show()

add axis labels using axes set

The code for this article is available on GitHub

We passed the xlabel and ylabel arguments to the axes.set() method to set labels for the x and y axes.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.