Matplotlib的概念这里就不多介绍了,关于绘图库Matplotlib的安装方法:点击这里
小编之前也和大家分享过python使用matplotlib实现的折线图和制饼图效果,感兴趣的朋友们也可以点击查看,下面来看看python使用matplotlib绘制柱状图的方法吧,具体如下:
1. 基本的柱状图
import matplotlib.pyplot as plt
data = [5, 20, 15, 25, 10]
plt.bar(range(len(data)), data)
plt.show()
plt.bar函数签名为:
bar(left, height, width=0.8, bottom=None, **kwargs)
事实上,left,height,width,bottom这四个参数确定了柱体的位置和大小。默认情况下,left为柱体的居中位置(可以通过align参数来改变left值的含义),即:
(left - width / 2, bottom)为左下角位置
(left + width / 2, bottom + height)为右上角位置
例如:
import matplotlib.pyplot as plt
data = [5, 20, 15, 25, 10]
plt.bar([0.3, 1.7, 4, 6, 7], d