matplotlib画柱状图十分简单,首先需要导入matplotlib中的pyplot,这个就是能够画图的类啦
import matplotlib.pyplot as plt
画柱状图十分简单,使用bar方法,比如画一个各个班级的人数吧,先准备一些数据,有两组数据,students为人数,这里放到y坐标系中,变量x就放在x坐标系中
students = [42,51,30,49] x = [1,2,3,4]
好了,现在我们可以直接画出来,调用bar方法,最后再调用show就能画图啦,代码如下
plt.bar(x=x, height=students) plt.show()
完整代码如下
#coding:utf-8 import matplotlib.pyplot as plt students = [42,51,30,49] x = [1,2,3,4] plt.bar(x=x, height=students) plt.show()
效果是这样的

你可能还喜欢下面这些文章