Skip to main content

How to write two texts inside multiple bars in a horizontal bar graph matplotlib?

My code so far gives me something that looks like this:

Horizontal Bar plot sample

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

Desired Horizontal Bar plot

Here's my base code so far:


import pandas
import matplotlib.pyplot as plt
import numpy as np

df = pandas.DataFrame(dict(graph=['Fruits & Vegetables','Bakery'],
                           p=[round(0.942871692205*100,5), round(0.933348641475*100,5)],
                           q=[round(0.941649166881*100,5),round(0.931458324883*100,5)],
                           r=[round(0.940803376393*100,5),round(0.929429068047*100,5)],
                          ))

ind = np.arange(len(df))
width = 0.25

fig, ax = plt.subplots()
ax.barh(ind, df.p, width, color='red', label='Accrate>=80')
ax.barh(ind + width, df.q, width, color='green', label='Accrate>=79')
ax.barh(ind + 2*width, df.r, width, color='blue', label='Accrate>=78')
                           
ax.set(yticks=ind + 2*width, yticklabels=df.graph, ylim=[2*width - 1, len(df)])
ax.legend(bbox_to_anchor=(1.1, 1.05))

plt.show()

I'd like to put the number text as shown in the desired plot link! How do I do that?



source https://stackoverflow.com/questions/76067718/how-to-write-two-texts-inside-multiple-bars-in-a-horizontal-bar-graph-matplotlib

Comments