4

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

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

3

You can use DataFrame.set_index
Pandas will use the index of your DataFrame as your labels by default.

df10.set_index('Wort').Vorkommen.plot(kind='barh')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.