kknight
kknight

Reputation: 43

Customizing labels in bar graph with Pandas

I've looked at several answers online, and can't seem to get what I want. Here's a sample data frame and plot:

df = pd.DataFrame({'Abbreviation': ['a', 'b', 'c', 'd', 'e', 'f', 'g'], 'Score 1': [1, 2, 3, 4.5, 6, None, None], 'w1': [1.5, 2.5, 3.5, 4.5, 6.5, None, None], 'Score 2': [None, None, 1.6, 3.5, 7.2, 3.4, 5.8], 'w2': [None, None, 6, 5, 4, 3, 2]})

ax = df.plot(x = 'Abbreviation', y = ['w1', 'w2'], kind = 'barh')

How would I provide annotations using only the "Score 1" column of my data frame? So in the example plot, this would annotate only the data associated with w1.

I've tried variations on this:

for p in ax.patches:
  ax.annotate(("%.2f" % p.get_width(), (p.get_x() + p.get_width(), p.get_y()), xytext=(5, 10), textcoords='offset points')

But where I get stuck is the where I provide ax.annotate with the list of text I want to use (the data contained in column "Score 1").

I've also tried variations on this as well:

for (x, y, z) in zip(df['Abbreviation'], df['w1'], df['Score 1']):
  ax.annotate('{:.0f}'.format(z), xy = (x,y,), xytext=(5, 10), textcoords='offset points')

But this throws an error because I'm passing a bunch of letters to the x coordinates.

I feel like the solution must be much simpler than I'm trying to make it. What am I missing?

Upvotes: 1

Views: 157

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210812

ax = df.plot(x = 'Abbreviation', y = ['w1', 'w2'], kind = 'barh')

for i,p in enumerate(ax.patches):
    if i >= len(df):
        break
    if df.at[df.index[i], 'Score 1']:
        ax.annotate(df.fillna('').at[df.index[i], 'Score 1'], 
                    (p.get_x() + p.get_width(), p.get_y()),
                    xytext=(5, 10), 
                    textcoords='offset points')

Result:

enter image description here

Upvotes: 2

Related Questions