Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have two overlaid bar plots from binary column data. I need to add the value_counts number as label to each bar of the plot.

df = pd.DataFrame({'y_pred': y_pred, 'y_test': y_test})

df.y_test.value_counts().plot(kind='bar')

df.y_pred.value_counts().plot(kind='bar', width=0.3, color='r')

plt.legend()

plt.show()

1 Answer

0 votes
by (25.1k points)

Your code is almost correct you need to pass ax into the plot function, likr this:

fig, ax = plt.subplots()

df.y_test.value_counts().plot(kind='bar', ax=ax)

df.y_pred.value_counts().plot(kind='bar', width=0.3, color='r', ax=ax)

for p in ax.patches:

ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005))

 plt.legend()

plt.show()

Output:

image

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...