1

I have the following data:

similarity_score_summary={'sim_model':['jaccard_sim','Cos_tfidf_sim','spacy_sim','sent_trf_sim','wv_gensim_sim'],
                          'acc(thres=0.5)':[0.65,0.66,0.38,0.62,0.39],
                          'acc(thres=0.75)':[0.61,0.62,0.41,0.79,0.44],
                          'acc(thres=0.90)':[0.62,0.62,0.60,0.76,0.55]
                }

df_sim = pd.DataFrame.from_dict(similarity_score_summary)
# setting first name as index column 
df_sim.set_index("sim_model", inplace = True) 

fig=plt.figure(figsize=(10, 8))
axes=fig.add_subplot(1, 1, 1)
bar_width = 0.3
idx = np.array(range(df_sim.shape[0]))
labels = df_sim.index
plt.bar(data=df_sim, height='acc(thres=0.5)', x=idx, color='b', width=bar_width, label='acc(thres=0.5)')
plt.bar(data=df_sim, height='acc(thres=0.75)', x=idx+bar_width, color='g', width=bar_width, label='acc(thres=0.75)')
plt.bar(data=df_sim, height='acc(thres=0.90)', x=idx+bar_width+bar_width, color='magenta', width=bar_width, label='acc(thres=0.90)')
plt.xticks(idx, labels, rotation=90)
plt.xlabel('Similarity methods')
plt.ylabel('Similarity Score')
plt.ylim((0, 1))
plt.title('Accuracy score for different Similarity methods on Quora Question pairs Kaggle data')
plt.legend(['thres=0.5', 'thres=0.75', 'thres=0.9'], loc='upper right', ncol = 3)
plt.show()

This results in below fig:

enter image description here

I need to add values on top of each bar. Need help?

Nishant
  • 1,063
  • 13
  • 40
  • There are a number of issues with this implementation, including that you created `axes` and then didn't correctly use the object (e.g. `axes.bar(...)` instead of `plt.bar(...)`). The correct current way to add annotations is with: `for c in axes.containers: axes.bar_label(c, fmt='%.2f', label_type='edge', padding=1)` – Trenton McKinney Nov 29 '21 at 16:42
  • The correct way to do this is plot `df_sim` directly which reduces the plot and annotation code to only 6 lines (compared to 25 shown in the accepted answer): see [code and plot](https://i.stack.imgur.com/ToWH1.png) – Trenton McKinney Nov 29 '21 at 16:53

1 Answers1

2

Define bars, iterate over them and get height of each bar, get x value and use plt.text to add value above each bar.

Example:

bar1 = plt.bar(
    data=df_sim,
    height="acc(thres=0.5)",
    x=idx,
    color="b",
    width=bar_width,
    label="acc(thres=0.5)",
)
bar2 = plt.bar(
    data=df_sim,
    height="acc(thres=0.75)",
    x=idx + bar_width,
    color="g",
    width=bar_width,
    label="acc(thres=0.75)",
)
bar3 = plt.bar(
    data=df_sim,
    height="acc(thres=0.90)",
    x=idx + bar_width + bar_width,
    color="magenta",
    width=bar_width,
    label="acc(thres=0.90)",
)
for bars in [bar1, bar2, bar3]:
    for bar in bars:
        yval = bar.get_height()
        plt.text(bar.get_x() + bar.get_width() / 2.0, yval + 0.005, yval, ha="center")

Result:

enter image description here

vladsiv
  • 2,718
  • 1
  • 11
  • 21