Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change Y axis on a pandas dataframe plot

Tags:

python

pandas

I have the following list:

[('und', 52),
 ('die', 36),
 ('von', 33),
 ('der', 29),
 ('in', 20),
 ('das', 17),
 ('für', 17),
 ('eine', 15),
 ('des', 14),
 ('Die', 14),
 ('ODP', 14),
 ('wurde', 13),
 ('zu', 13)]

With the following code I convert it to a pandas dataframe:

df = pd.DataFrame(sorted_by_value, columns=['Wort', 'Vorkommen'])

Then I plot it:

fig, ax_lst = plt.subplots(1,1)  # a figure with a 2x2 grid of Axes
df10 = df[:20]
df10.Vorkommen.plot(kind='barh')
plt.show()

How can I change the y axis from the index to the words?

enter image description here

like image 701
madik_atma Avatar asked Mar 04 '23 07:03

madik_atma


1 Answers

By default plot.barh uses the index. You can explicitly set the x and y values like so:

df.plot.barh(x='Wort', y='Vorkommen')

enter image description here

like image 128
iacob Avatar answered Mar 21 '23 10:03

iacob