Home »
Python »
Python Programs
How to add x and y labels to a pandas plot?
Given a Pandas plot, we have to add x and y labels.
Submitted by Pranit Sharma, on May 18, 2022
For setting indices, we need to set the x and y labels according to the following syntax:
set_xlabel('label name')
set_ylabel('label name')
To work with pandas, we need to import pandas package first, below is the syntax:
import pandas as pd
Let us understand with the help of an example.
# Importing pandas package
import pandas as pd
# creating a dictionary
d = {
"Players":['Sachin','Ganguly','Dravid','Yuvraj','Dhoni','Kohli'],
"Runs":[18426,11363,10889,8701,10773,12311]
}
# Now we will create DataFrame
df = pd.DataFrame(d)
# Viewing the DataFrame
print("Original DataFrame:\n",df,"\n\n")
plot = df.plot(lw=2, colormap='jet', marker='.', markersize=10,
title='Players and their runs')
plot.set_xlabel("X")
plot.set_ylabel("Y")
print(plot)
Output:
Python Pandas Programs »