Skip to content
Advertisement

How to write text inside the bar in a horizontal bar graph matplotlib?

My code so far gives me something that looks like this: enter image description here

I am wanting to write in the bars themselves if possible to get something like this: enter image description here

Here’s my base code so far:

I’d like to put Disease into the bar

JavaScript

Advertisement

Answer

You can add text to the ax via matplotlib as ax.text().

If you add

JavaScript

just before plt.show() in your code, you’ll get:

enter image description here

Note, I had to reverse your Disease list (using [::-1]) for text to appear the way you have in your image, otherwise Amyotrophic Lateral Sclerosis is on top, and Acute Treatment of Migraine on the bottom.

Function generic form comes in plt.text(x, y, text), so you see I offset x by 0.1 and get y from the bar patch.

For extra information on the function, you can check out matplotlib documentation here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement