我尝试制作条形图的组,每个条形图的顶部都带有标签,但是标签总是越过框架。我想使标签不越过框架,但标签仍位于栏的顶部。 my bar
def autolabel(rects):#label di atas bar
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0,3), # 3 points vertical offset
textcoords='offset points',
ha='center',va='bottom')
plt.rcParams['figure.figsize'] = 6, 10. #w,h
labels = ['1', '2', '3', '4','5']
men_means = [20, 34, 30, 35, 98]
women_means = [25, 32, 86, 20, 25]
shemale_means = [45, 37, 95, 25, 22]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig1, ax1 = plt.subplots(ncols=1, nrows=4, constrained_layout=True)
for ax in ax1.flat:
rects1 = ax.bar(x - width/2, men_means, width, label='Testing Prediction')
rects2 = ax.bar(x+ width/2, women_means, width, label='Training Prediction')
ax.set_title('Title', fontsize=12)
ax.set_ylabel('Scores')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.tick_params(axis="x", labelsize=8)
ax.set_ylim(bottom=0, top=100)
autolabel(rects1)
autolabel(rects2)