函数原型如下
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align=‘center’, data=None, **kwargs)
x 表示x坐标,数据类型为int或float类型。
height 表示柱状图的高度,也就是y坐标值,数据类型为int或float类型。
width 表示柱状图的宽度,取值在0~1之间,默认为0.8。
bottom 柱状图的起始位置,也就是y轴的起始坐标。
align 柱状图的对齐方式,默认为’center’(以x坐标为中心),‘edge’:将条形的左边缘与x对齐。
color 柱状图颜色
edgecolor 边框颜色
linewidth 边框宽度
tick_label 下标标签
简单应用:
import matplotlib.pyplot as plt
x = ['apple','banana','pear','watermalon']
y = [40,52,25,33]
#指定显示中文
plt.rcParams['font.sans-serif']=['SimHei']#指定默认字体,不指定中文可能无法正常显示
plt.rcParams['axes.unicode_minus']=False#解决保存图像是负号'-'显示为方块的问题
plt.figure(figsize=(10,7))
plt.bar(range(len(x)),y,tick_label=x,color='blue',edgecolor='white',width=0.5)
plt.xticks(rotation=45,fontsize=15)#调节横坐标倾斜度
plt.yticks(fontsize=15)#纵坐标字号
plt.legend()#图例
plt.title('''水果销量''',fontsize=20)#标题
plt.show()