Add x and y labels to a pandas plot
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Romantic Lands Beckon
--
Chapters
00:00 Add X And Y Labels To A Pandas Plot
00:33 Answer 1 Score 444
01:12 Answer 2 Score 64
01:37 Answer 3 Score 33
02:09 Accepted Answer Score 32
02:28 Answer 5 Score 31
02:55 Thank you
--
Full question
https://stackoverflow.com/questions/2148...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #dataframe #matplotlib
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Romantic Lands Beckon
--
Chapters
00:00 Add X And Y Labels To A Pandas Plot
00:33 Answer 1 Score 444
01:12 Answer 2 Score 64
01:37 Answer 3 Score 33
02:09 Accepted Answer Score 32
02:28 Answer 5 Score 31
02:55 Thank you
--
Full question
https://stackoverflow.com/questions/2148...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #dataframe #matplotlib
#avk47
ANSWER 1
Score 447
The df.plot() function returns a matplotlib.axes.AxesSubplot object. You can set the labels on that object.
ax = df2.plot(lw=2, colormap='jet', marker='.', markersize=10, title='Video streaming dropout by category')
ax.set_xlabel("x label")
ax.set_ylabel("y label")

Or, more succinctly: ax.set(xlabel="x label", ylabel="y label").
Alternatively, the index x-axis label is automatically set to the Index name, if it has one. so df2.index.name = 'x label' would work too.
ANSWER 2
Score 64
You can use do it like this:
import matplotlib.pyplot as plt
import pandas as pd
plt.figure()
values = [[1, 2], [2, 5]]
df2 = pd.DataFrame(values, columns=['Type A', 'Type B'],
index=['Index 1', 'Index 2'])
df2.plot(lw=2, colormap='jet', marker='.', markersize=10,
title='Video streaming dropout by category')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
plt.show()
Obviously you have to replace the strings 'xlabel' and 'ylabel' with what you want them to be.
ANSWER 3
Score 33
If you label the columns and index of your DataFrame, pandas will automatically supply appropriate labels:
import pandas as pd
values = [[1, 2], [2, 5]]
df = pd.DataFrame(values, columns=['Type A', 'Type B'],
index=['Index 1', 'Index 2'])
df.columns.name = 'Type'
df.index.name = 'Index'
df.plot(lw=2, colormap='jet', marker='.', markersize=10,
title='Video streaming dropout by category')

In this case, you'll still need to supply y-labels manually (e.g., via plt.ylabel as shown in the other answers).
ANSWER 4
Score 31
It is possible to set both labels together with axis.set function. Look for the example:
import pandas as pd
import matplotlib.pyplot as plt
values = [[1,2], [2,5]]
df2 = pd.DataFrame(values, columns=['Type A', 'Type B'], index=['Index 1','Index 2'])
ax = df2.plot(lw=2,colormap='jet',marker='.',markersize=10,title='Video streaming dropout by category')
# set labels for both axes
ax.set(xlabel='x axis', ylabel='y axis')
plt.show()
