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:
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With