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')
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')
Notice that a label has been placed on the y-axis but no label has been placed on the x-axis since we didn’t use the xlabel argument.
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