条形图加数字显示

import matplotlib.pyplot as plt
import pandas as pd

data = pd.Series([4, 5, 6], index=['A','B','C'])
fig = plt.figure(figsize=(7, 5), dpi=90) # 声明画布1
ax = fig.add_subplot(1,1,1) # 声明绘图区
x, y = data.index, data.values
rects = plt.bar(x, y, color='dodgerblue', width=0.35, label='label1')
plt.grid(linestyle="-.", axis='y', alpha=0.4)
plt.legend()
plt.tight_layout()

for rect in rects: #rects 是三根柱子的集合

height = rect.get_height()

plt.text(rect.get_x() + rect.get_width() / 2, height,

str(height),

size=15,

ha='center',

va='bottom')

for a,b in zip(x,y):
plt.text(a, b-0.2,'%.2f'%b, ha = 'center',va = 'bottom',fontsize=15)
plt.show()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。