如何解决条形图中带注释的值的重叠

时间:2020-04-12 05:51:25

标签: matplotlib bar-chart google-colaboratory

我绘制了一个条形图,然后尝试注释该图中每个条形的值。问题是注释值重叠。那么,如何解决此问题? enter image description here 这就是密码

plt.rcParams.update({'font.size':16})

labels = df['Training Period']
all_parameter= df['Predicted Cumulative Light Energy including all weather parameters']
temperature_parameter= df['Predicted Cumulative Light Energy including Temperature Only']

x=np.arange(len(labels))
width =0.4
fig, ax= plt.subplots(figsize=(10,6))

rects1= ax.bar(x-width/2, all_parameter, width, label= 'prediction with all parameter included')
rects2= ax.bar(x+width/2, temperature_parameter, width, label ='prediction with Temperature (C) only')

ax.set_ylabel('Cumulative Light Energy (Wh/m^2)')
ax.set_xlabel('Training Period')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend(fontsize=24)
ax.legend(loc='lower center')

def autolabel(rects):
  for rect in rects:
    height=rect.get_height()
    ax.annotate('{}'.format(height), xy=(rect.get_x() + rect.get_width()/2, height),xytext=(0,1), textcoords='offset points', ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

fig.tight_layout()
plt.show()

0 个答案:

没有答案