后浪云Python教程:python如何绘柱状图

后浪云Python教程:python如何绘柱状图插图

使用Python中的Matplotlib画基本的柱状图

import matplotlib.pyplot as plt

data = [5, 20, 15, 25, 10]

plt.bar(range(len(data)), data)
plt.show()

后浪云Python教程:python如何绘柱状图插图1

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], data, width=0.6, bottom=[10, 0, 5, 0, 5])
plt.show()

后浪云Python教程:python如何绘柱状图插图2

推荐学习《Python教程》!

THE END