matplotlib 绘制条形图

代码部分:

import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt

x = [15, 20, 30]
text_tag_lst = ['blue','green', 'black']
myfont = matplotlib.font_manager.FontProperties(fname="/usr/share/fonts/truetype/arphic/uming.ttc")
y_pos = np.arange(len(x)) * 10 + 6
size = max(x) + 10, max(y_pos) * 11
dpi = 80.0
#注意figsize的计算,需要除去dpi
figsize = size[0]*11/dpi, size[1]/float(dpi)
fig = plt.figure(figsize=figsize, dpi=dpi)
fig.patch.set_alpha(0)

x_pos = np.array(x)
error = np.random.rand(len(x))
plt.barh(y_pos, x_pos , xerr=error, height=4,align='center', alpha=0.4)
plt.yticks(y_pos, text_tag_lst, fontproperties=myfont)
plt.savefig(save_path)

matplotlib 绘制条形图_第1张图片

代码说明:
matplotlib.use(‘Agg’) 是为了解决:
matplotlib.font_manager.FontProperties(fname=”/usr/share/fonts/truetype/arphic/uming.ttc”)”是为了解决在matplotlib上显示中文问题

变量参数是说明:
x,就是每个柱体的长度,这里需要先将list的x转成np.array 类型
height,就是每个柱体的宽度
barh 是绘制条形图的主方法
yticks 方法用于对每个柱体打上text标签
savefig 保存图片
函数原型:
matplotlib.pyplot.bar(left, height, width=0.8, bottom=None, hold=None, **kwargs)

你可能感兴趣的:(python)