代码示例:
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
my_font = fm.FontProperties(fname='/System/Library/Fonts/PingFang.ttc',size=10)
a = ['流浪地球', '疯狂的外星人','飞驰人生','大黄蜂','熊出没.原始时代','新喜剧之王']
b = [38.13, 19.85, 14.89, 11.36,6.47,5.93]
plt.figure(figsize=(20,8),dpi=80)
rects = plt.barh(range(len(a)),b,height=0.5,color='r')
plt.yticks(range(len(a)),a,fontproperties=my_font,rotation=45)
for rect in rects:
wd = rect.get_width()
plt.text(wd,rect.get_y()+0.5/2,str(wd),va='center')
plt.show()
效果截图: