python - How to display the value of the bar on each bar with pyplot.barh()? -
i generated bar plot, how can display value of bar on each bar?
current plot:
what trying get:
my code:
import os import numpy np import matplotlib.pyplot plt x = [u'info', u'cuisine', u'type_of_place', u'drink', u'place', u'meal_time', u'dish', u'neighbourhood'] y = [160, 167, 137, 18, 120, 36, 155, 130] fig, ax = plt.subplots() width = 0.75 # width of bars ind = np.arange(len(y)) # x locations groups ax.barh(ind, y, width, color="blue") ax.set_yticks(ind+width/2) ax.set_yticklabels(x, minor=false) plt.title('title') plt.xlabel('x') plt.ylabel('y') #plt.show() plt.savefig(os.path.join('test.png'), dpi=300, format='png', bbox_inches='tight') # use format='svg' or 'pdf' vectorial pictures
add:
for i, v in enumerate(y): ax.text(v + 3, + .25, str(v), color='blue', fontweight='bold')
result:
the y-values v
both x-location , string values ax.text
, , conveniently barplot has metric of 1 each bar, enumeration i
y-location.
Comments
Post a Comment