Q:

How to add x and y labels to a pandas plot?

belongs to collection: Python Pandas Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

Example: Add x and y labels to a pandas plot

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to find row where values for column is maximal... >>
<< How to split a DataFrame string column into two co...