import matplotlib import matplotlib.pyplot as plt import numpy as np def autolabel(rects, xpos='center'): """ Attach a text label above each bar in *rects*, displaying its height. *xpos* indicates which side to place the text w.r.t. the center of the bar. It can be one of the following {'center', 'right', 'left'}. """ ha = {'center': 'center', 'right': 'left', 'left': 'right'} offset = {'center': 0, 'right': 1, 'left': -1} for rect in rects: height = rect.get_height() ax.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(offset[xpos]*3, 0), # use 3 points offset textcoords="offset points", # in both directions ha=ha[xpos], va='bottom') # plotting data plot_data = [20, 35, 30, 35, 27, 50, 35, 30, 35, 27] x_labels = ['G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7', 'G8', 'G9', 'G10'] index = np.arange(len(plot_data)) # the x locations for the groups width = 0.35 # the width of the bars fig, ax = plt.subplots(figsize=(15, 4.0)) rects = ax.bar(index, plot_data, width, label='Men') ax.set_box_aspect(0.2) ax.autoscale() # Add some text for labels, title and custom x-axis tick labels, etc. ax.set_ylabel('Scores') ax.set_title('Scores by group') ax.set_xticks(index) ax.set_xticklabels(x_labels) ax.legend() autolabel(rects, "center") fig.tight_layout() plt.margins(0.01, 0.1) plt.subplots_adjust(bottom=0.10) # plt.grid(True) plt.xticks(index, x_labels, rotation='vertical') plt.savefig('barplot.png', dpi=72) plt.show()