七月
29
matplotlib教程:柱状图
import numpy as np
import matplotlib.pyplot as plt
N = 4
y = [20, 10, 30, 25]
index = np.arange(N)
# color 颜色
# width 柱状图的宽度,默认0.8
# p1 = plt.bar(x=index, height=y, color='g', width=0.5)
# 横向的柱状图
# p2 = plt.barh(y=index, width=y, color='g', height=0.5)
# 一幅图中画多个柱状图
bar_width = 0.3
sales_BJ = [52, 53, 63, 53]
sales_SH = [44, 66, 55, 41]
# # 左右并列的柱状图
# plt.bar(index-0.5*bar_width, sales_BJ, bar_width, color='b')
# plt.bar(index+0.5*bar_width, sales_SH, bar_width, color='r')
# 上下层叠的柱状图
plt.bar(x=index, height=sales_BJ, width=bar_width, color='b')
plt.bar(x=index, bottom=sales_BJ, height=sales_SH, width=bar_width, color='r')
plt.show()