三 条形图
以长方形的长度为变量的统计图表,用来比较多个项目分类的数据大小
(1)柱状图
import numpy as np
import matplotlib.pyplot as plt
N = 5
y = np.array([20,10,30,25,15])
index = np.arange(N)
# 柱状图
plt.bar(index,y)
(2)条形柱状图
import numpy as np
import matplotlib.pyplot as plt
N = 5
y = np.array([20,10,30,25,15])
index = np.arange(N)
# 条形柱状图
plt.barh(index,y)
(3)堆叠柱状图
import numpy as np
import matplotlib.pyplot as plt
sales_BJ = np.array([52,55,63,53])
sales_SH = np.array([44,66,55,41])
bar_width=0.3
index = [0.0,1.0,2.0,3.0]
plt.bar(index,sales_BJ,bar_width,color='b')
plt.bar(index,sales_SH,bar_width,color='r')
(4)并列柱状图
import numpy as np
import matplotlib.pyplot as plt
sales_BJ = np.array([52,55,63,53])
sales_SH = np.array([44,66,55,41])
bar_width=0.3
index = [0.0,1.0,2.0,3.0]
plt.bar(index,sales_BJ,bar_width,color='b')
for i in range(len(index)):
index[i] += bar_width
plt.bar(index,sales_SH,bar_width,color='r')