matplotlib画图、表一起显示

1、图表一起

1.1、代码

    import numpy as np
    import matplotlib.pyplot as plt

    labels = ['a', 'longlonglong', 'bbb', 'ccc', 'dddddddd', 'eeeee', 'ffff', 'ggggggggg']

    step1 = [0.75, 1.22, 20.27, 0.49, 5.52, 11.76, 2.30, 0.64]
    step2 = [0.89, 3.62, 18.69, 0.22, 9.61, 14.06, 1.28, 0.27]
    code_size = [step1, step2]
    width = 0.8

    x = np.arange(len(labels))  # the label locations
    # 条形宽度
    width = 0.5  # the width of the bars

    fig, ax = plt.subplots(layout='constrained')

    count = 1
    for step in code_size:
        ax.bar(labels, step, width, label='Step ' + str(count))
        count += 1
    ax.set_ylabel('Time used (s)')
    ax.set_title('Time and size')
    ax.set_xticks(x + width, [], fontsize=10)
    ax.legend()
    plt.xticks(rotation=45)

    plt.table(cellText=code_size,
              rowLabels=['row title1', 'row title2'],
              colLabels=labels,
              cellLoc='center',
              loc='bottom')

    plt.subplots_adjust(bottom=0.05)
    plt.show()

1.2、显示效果

 

2、图表分离

2.1、代码

    import numpy as np
    import matplotlib.pyplot as plt

    labels = ['a', 'longlonglong', 'bbb', 'ccc', 'dddddddd', 'eeeee', 'ffff', 'ggggggggg']
    code_size = ['5KB', '83KB', '1.7MB', '18KB', '1MB', '18KB', '4MB', '55KB']

    step1 = [0.75, 1.22, 20.27, 0.49, 5.52, 11.76, 2.30, 0.64]
    step2 = [0.89, 3.62, 18.69, 0.22, 9.61, 14.06, 1.28, 0.27]
    width = 0.8

    fig, (ax, ax_table) = plt.subplots(nrows=2, gridspec_kw=dict(height_ratios=[3, 1]))

    ax_table.axis('off')
    ax.bar(labels, step1, width, label='Step 1')
    ax.bar(labels, step2, width, bottom=step1, label='Step 2')
    ax.set_ylabel('Time used (s)')
    ax.set_title('Time and size')
    ax.legend()
    ax.tick_params(axis='x', labelrotation=45)

    ax_table = plt.table(cellText=[code_size],
                         rowLabels=['Code size'],
                         loc='bottom')

    plt.subplots_adjust(bottom=0.05)
    plt.show()

2.2、显示效果

 

 

posted @ 2024-06-18 11:18  小粉优化大师  阅读(2)  评论(0编辑  收藏  举报