1

I'm drawing a bar chart with Pandas but I want to draw the value of each bar on the bar itself. How can I do that please?

Here is an example of what I want to achieve, but of course I want the number to be vertical on the bar not horizontal as I have here.

enter image description here

Jack Twain
  • 6,273
  • 15
  • 67
  • 107

1 Answers1

0

Here is an example:

import pandas as pd
import matplotlib.pyplot as plt
ix3 = pd.MultiIndex.from_arrays([['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b'], ['foo', 'foo', 'bar', 'bar', 'foo', 'foo', 'bar', 'bar']], names=['letter', 'word'])
df3 = pd.DataFrame({'data1': [3, 2, 4, 3, 2, 4, 3, 2], 'data2': [6, 5, 7, 5, 4, 5, 6, 5]}, index=ix3)
gp3 = df3.groupby(level=('letter', 'word'))
means = gp3.mean()
errors = gp3.std()
fig, ax = plt.subplots()
means.plot(yerr=errors, ax=ax, kind='bar')

for rect in ax.patches:
    bbox = rect.get_bbox()
    x = 0.5 * (bbox.x0 + bbox.x1)
    y = 0.5 * (bbox.y0 + bbox.y1)
    text = "{:g}".format(bbox.y1)
    ax.text(x, y, text,
            va="center", ha="center", 
            rotation=90, fontsize="x-large", color="w")

the outtput:

enter image description here

HYRY
  • 94,853
  • 25
  • 187
  • 187