并列柱状图的通用方法
参考链接:https://www.jb51.net/article/206226.htm
- datas :数据集,二维列表,要求列表每个元素的长度必须与labels的长度一致
- title:标题
- x_labels : x轴坐标标签序列
- y_label:y轴坐标标签
- tick_step :默认x轴刻度步长为1,通过tick_step可调整x轴刻度步长。
- group_gap : 柱子组与组之间的间隙,最好为正值,否则组与组之间重叠
- bar_gap :每组柱子之间的空隙,默认为0,每组柱子紧挨,正值每组柱子之间有间隙,负值每组柱子之间重叠
代码如下:
def create_multi_bars(datas, title, x_labels, y_label, bar_labels, tick_step=1, group_gap=0.2, bar_gap=0):
x = np.arange(len(x_labels)) * tick_step
group_num = len(datas)
group_width = tick_step - group_gap
bar_span = group_width / group_num
bar_width = bar_span - bar_gap
for index, y in enumerate(datas):
plt.bar(x + index * bar_span, y, bar_width, label=bar_labels[index])
plt.ylabel(y_label)
plt.title(title)
ticks = x + (group_width - bar_span) / 2
plt.xticks(ticks, x_labels)
plt.legend()
plt.show()
if __name__ == '__main__':
size = 4
a = np.random.random(size)
b = np.random.random(size)
c = np.random.random(size)
d = np.random.random(size)
data = [a,b,c,d]
bar_labels = ['bar1', 'bar2', 'bar3', 'bar4']
x_labels = ['x1', 'x2', 'x3', 'x4']
title = 'multi bars'
y_label = 'y_label'
create_multi_bars(data, title, x_labels, y_label, bar_labels)
结果如下:
|