3

I am using:

plt.bar(range(len(d)), d.values(), align='center')
plt.yticks(range(len(d)), d.keys())
plt.xlabel('frequency', fontsize=18)
plt.ylabel('keywords', fontsize=18)

plt.show()

and I am getting the output below: enter image description here

but I want to show the corresponding bar for each keyword on the y-axis, rather than the x-axis. How can I achieve that?

3
  • read matplot doc: matplot bar() Commented Oct 24, 2016 at 17:20
  • It looks like you are setting yticks the keys of the dictionary. Maybe you want to do that for xticks? Commented Oct 24, 2016 at 17:40
  • But then it will plot keywords on x-axis. I have to plot keywords on y-axis and corresponding horizontal bar with corresponding values on x-axis. Commented Oct 24, 2016 at 17:45

2 Answers 2

6

Using matplotlib's barh() function will create a horizontal bar chart for you:

plt.barh(range(len(d)), d.values(), align='center')

From the docs:

matplotlib.pyplot.barh(bottom, width, height=0.8, left=None, hold=None, **kwargs)

Make a horizontal bar plot with rectangles bounded by:

left, left + width, bottom, bottom + height

(left, right, bottom and top edges)

bottom, width, height, and left can be either scalars or sequences

Also see the barh demo on the matplotlib gallery page here

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

1 Comment

Thankyou for the answer.. yeah everytime i was missing barh.
0

Use plt.barh() rather than using the vertical bar chart function.

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.