0
点赞
收藏
分享

微信扫一扫

并列柱状图

迪莉娅1979 2022-04-24 阅读 81
python
# -*- coding: UTF-8 -*-
"""
@author:lian
"""
import matplotlib.pyplot as plt
import numpy as np
import streamlit as st

# 构造数据时要注意对应关系,顺序必须一致
label = ['平板', '筏板', '矩形梁', '雨篷', '汽车坡道']
first = np.random.rand(5)
second = np.random.rand(5)
third = np.random.rand(5)
data = [first, second, third]


def autolabel(rects, plt_):
    for rect in rects:
        height = rect.get_height()
        plt_.annotate(f'{height:.2f}',
                      xy=(rect.get_x() + rect.get_width() / 2, height),
                      xytext=(0, 3),
                      textcoords="offset points",
                      ha='center', va='bottom')


def create_multi_bars(datas, labels, x_label, y_label, tick_step=1, group_gap=0.2, bar_gap=0):
    ticks = np.arange(len(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
    baseline_x = ticks - (group_width - bar_span) / 2
    for index, y in enumerate(datas):
        tmp = plt.bar(baseline_x + index * bar_span, y, bar_width)
        autolabel(tmp, plt)

    plt.xlabel(x_label)
    plt.ylabel(y_label)
    plt.xticks(ticks, labels)
    return plt


if __name__ == '__main__':
    a = create_multi_bars(data, label, '清单项', '精确率')
    st.pyplot(a)

举报

相关推荐

0 条评论