如何在水平条形图上显示值

11 浏览
0 Comments

如何在水平条形图上显示值

我生成了一个条形图,如何在每个条形上显示条形的值?

当前的图:

enter image description here

我想要的效果:

enter image description here

我的代码:

import os
import numpy as np
import matplotlib.pyplot as 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 # 条形的宽度
ind = np.arange(len(y))  # 组的 x 位置
ax.barh(ind, y, width, color="blue")
ax.set_yticks(ind+width/2)
ax.set_yticklabels(x, minor=False)
plt.title('标题')
plt.xlabel('x')
plt.ylabel('y')      
#plt.show()
plt.savefig(os.path.join('test.png'), dpi=300, format='png', bbox_inches='tight') # 使用 format='svg' 或 'pdf' 生成矢量图片

0