How can we add axis labels to plots in Pandas? 2

How can we add axis labels to plots in Pandas?

Adding axis labels to plots in Pandas is a simple and straightforward process that can greatly enhance the visual presentation of data. To add axis labels, first, create the desired plot using the .plot() function in Pandas. Then, use the .set_xlabel() and .set_ylabel() functions to specify the labels for the x-axis and y-axis, respectively. These functions allow for customization of the label text, font, size, and other properties. By adding axis labels, the plot becomes more informative and easier to interpret, making it a valuable tool for data analysis and communication.

Add Axis Labels to Plots in Pandas (With Examples)


You can use the following basic syntax to add axis labels to a plot in pandas:

df.plot(xlabel='X-Axis Label', ylabel='Y-Axis Label')

The following example shows how to use this syntax in practice.

Example: Add Axis Labels to Plot in Pandas

Suppose we have the following pandas DataFrame that shows the total sales made at three stores during consecutive days:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'store1_sales': [4, 7, 9, 12, 10, 14, 16, 19, 22, 25],
                   'store2_sales': [3, 3, 4, 6, 7, 6, 8, 10, 14, 19],
                   'store3_sales': [2, 2, 4, 2, 5, 5, 6, 8, 8, 11]})

#view DataFrame
print(df)

   store1_sales  store2_sales  store3_sales
0             4             3             2
1             7             3             2
2             9             4             4
3            12             6             2
4            10             7             5
5            14             6             5
6            16             8             6
7            19            10             8
8            22            14             8
9            25            19            11

If we create a plot to visualize the sales by store, the pandas plot() function will not add axis labels to the plot by default:

#plot sales by store
df.plot()

To add axis labels, we must use the xlabel and ylabel arguments in the plot() function:

#plot sales by store, add axis labels
df.plot(xlabel='Day', ylabel='Sales')

pandas plot axis labels

Notice that the x-axis and y-axis now have the labels that we specified within the plot() function.

Note that you don’t have to use both the xlabel and ylabel arguments.

For example, you may choose to only add a label to the y-axis:

#plot sales by store, add label to y-axis only
df.plot(ylabel='Sales')

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

Cite this article

stats writer (2024). How can we add axis labels to plots in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-we-add-axis-labels-to-plots-in-pandas/

stats writer. "How can we add axis labels to plots in Pandas?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-we-add-axis-labels-to-plots-in-pandas/.

stats writer. "How can we add axis labels to plots in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-we-add-axis-labels-to-plots-in-pandas/.

stats writer (2024) 'How can we add axis labels to plots in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-we-add-axis-labels-to-plots-in-pandas/.

[1] stats writer, "How can we add axis labels to plots in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can we add axis labels to plots in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top