Python · matplotlib | 简单的柱状图画图代码



human like sepoa

import matplotlib.pyplot as plt
import numpy as np

if __name__ == '__main__':
    # 改字体大小
    plt.rcParams.update({'font.size': 12})
    # 不要使用 type 3 字体
    plt.rcParams.update({'pdf.fonttype': 42})

    # 数据
    tasks = ['Cheetah_run', 'Walker_run', 'Quadruped_walk', 'Quadruped_run']
    methods = ['Method', 'SURF', 'PEBBLE', 'RUNE']
    data = np.array([
        [[16, 10, 11, 11], [18, 9, 11, 11], [17, 12, 14, 10]],  # Cheetah_run
        [[16, 9, 11, 11], [19, 10, 11, 11], [18, 10, 12, 10]],  # Walker_run 
        [[17, 10, 12, 10], [16, 9, 12, 11], [19, 10, 12, 10]],  # Quadruped_walk
        [[15, 10, 12, 9], [16, 9, 13, 11], [17, 10, 12, 7]]   # Quadruped_run
    ])
    data = data * 5
    mean_data = np.mean(data, axis=1)
    variance = np.var(data, axis=1)
    variance = np.sqrt(variance)

    # 绘图
    fig, ax = plt.subplots(figsize=(7, 2.5))

    # 设置条形图的位置和宽度
    width = 0.2
    true_width = 0.16
    x = np.arange(len(tasks))  # x轴的位置

    # 绘制条形图
    rects1 = ax.bar(x - width, mean_data[:, 0], true_width, label='S-EPOA', 
                    color='mistyrose', edgecolor='indianred', linewidth=1.8)
    rects2 = ax.bar(x, mean_data[:, 1], true_width, label='SURF', 
                    color='floralwhite', edgecolor='peru', linewidth=1.8)
    rects3 = ax.bar(x + width, mean_data[:, 2], true_width, label='PEBBLE', 
                    color='honeydew', edgecolor='mediumseagreen', linewidth=1.8)
    rects4 = ax.bar(x + 2 * width, mean_data[:, 3], true_width, label='RUNE', 
                    color='lavender', edgecolor='mediumpurple', linewidth=1.8)

    # 添加误差条(yerr 参数)
    ax.errorbar(x - width, mean_data[:, 0], yerr=variance[:, 0], fmt='none', color='indianred', capsize=5)
    ax.errorbar(x, mean_data[:, 1], yerr=variance[:, 1], fmt='none', color='peru', capsize=5)
    ax.errorbar(x + width, mean_data[:, 2], yerr=variance[:, 2], fmt='none', color='mediumseagreen', capsize=5)
    ax.errorbar(x + 2 * width, mean_data[:, 3], yerr=variance[:, 3], fmt='none', color='mediumpurple', capsize=5)

    # 添加标签和标题
    ax.set_ylabel('Match Ratio (%)', fontsize=14)
    # ax.set_title('Performance Comparison by Task and Method')
    ax.set_xticks(x + width / 2)  # 设置 x 轴标签的位置
    # ax.set_xticks(x)
    ax.set_xticklabels(tasks, fontname='FreeMono', rotation=0, ha='center')
    ax.set_ylim(0, 100)
    ax.set_yticks([0, 25, 50, 75, 100])
    ax.yaxis.grid(True, color='lightgray', linestyle='-', linewidth=1)
    ax.set_axisbelow(True)

    
    lines, labels = fig.axes[-1].get_legend_handles_labels()
    fig.legend(lines, labels, bbox_to_anchor=(0.5, 1.02), loc='center',
               ncol=4, prop={'size': 13}, frameon=False)

    # 显示图形
    plt.tight_layout()
    # plt.show()
    plt.savefig('./human_experiments/human_like_sepoa.png', bbox_inches='tight', pad_inches=0.2)
    plt.savefig('./human_experiments/human_like_sepoa.pdf', bbox_inches='tight', pad_inches=0.2)
    plt.close()

human get confused (sepoa)

import matplotlib.pyplot as plt
import numpy as np


if __name__ == '__main__':
    # 改字体大小
    plt.rcParams.update({'font.size': 12})
    # 不要使用 type 3 字体
    plt.rcParams.update({'pdf.fonttype': 42})

    # 数据
    tasks = ['Cheetah_run', 'Walker_run', 'Quadruped_walk', 'Quadruped_run']
    return_diff_10 = [53.33, 48.33, 55.83, 56.67]  # 对应 10
    return_diff_100 = [68.33, 73.33, 63.33, 70]  # 对应 100
    return_diff_500 = [95, 98.33, 71.67, 73.33]  # 对应 500

    # 计算上面三行值 / 5 的结果
    # return_diff_10 = [11, 10, 11, 11]
    # return_diff_100 = [13, 15, 13, 14]
    # return_diff_500 = [19, 20, 14, 14]

    # 生成条形图的 x 轴位置
    x = np.arange(len(tasks))  # 任务数目
    width = 0.25  # 条形宽度
    true_width = 0.18

    # 创建图形
    fig, ax = plt.subplots(figsize=(7, 2.5))

    # 绘制条形图
    rects1 = ax.bar(x - width, return_diff_500, true_width, label='Return Difference (RD) = 500', 
                    color='mistyrose', edgecolor='indianred', linewidth=1.8)  # hatch='//'
    rects2 = ax.bar(x, return_diff_100, true_width, label='RD = 100', 
                    color='floralwhite', edgecolor='peru', linewidth=1.8)  # hatch='\\'
    rects3 = ax.bar(x + width, return_diff_10, true_width, label='RD = 10', 
                    color='aliceblue', edgecolor='dodgerblue', linewidth=1.8)  # hatch='--'

    

    # 添加一些文本标签、标题和自定义 x 轴标签
    ax.set_ylim(0, 100)
    ax.set_yticks([0, 25, 50, 75, 100])
    ax.set_ylabel('Match Ratio (%)', fontsize=14)
    # ax.set_title('Return Differences by Task and Time')
    ax.set_xticks(x)
    ax.set_xticklabels(tasks, fontname='FreeMono', fontsize=12)  # , fontweight='bold'
    plt.xticks(rotation=0, ha='center')
    ax.yaxis.grid(True, color='lightgray', linestyle='-', linewidth=1)
    ax.set_axisbelow(True)


    # ax.legend(fontsize=12)
    lines, labels = fig.axes[-1].get_legend_handles_labels()
    fig.legend(lines, labels, bbox_to_anchor=(0.5, 1.02), loc='center',
               ncol=3, prop={'size': 13}, frameon=False)

    # 给条形图添加数值标签
    def add_labels(rects):
        for rect in rects:
            height = rect.get_height()
            ax.annotate(f'{height}%',
                        xy=(rect.get_x() + rect.get_width() / 2, height),
                        xytext=(0, 3),  # 3 points vertical offset
                        textcoords="offset points",
                        ha='center', va='bottom')

    # 为每一组条形图添加标签
    # add_labels(rects1)
    # add_labels(rects2)
    # add_labels(rects3)

    # 显示图形
    plt.tight_layout()
    # plt.show()
    plt.savefig('./human_experiments/human_get_confused.png', bbox_inches='tight', pad_inches=0.2)
    plt.savefig('./human_experiments/human_get_confused.pdf', bbox_inches='tight', pad_inches=0.2)
    plt.close()


human like clarify

import matplotlib.pyplot as plt
import numpy as np

if __name__ == '__main__':
    # 改字体大小
    plt.rcParams.update({'font.size': 14})
    # 不要使用 type 3 字体
    plt.rcParams.update({'pdf.fonttype': 42})

    # 数据
    method_name = 'CLARITY'
    tasks = ['dial-turn', 'hammer', 'walker-walk']
    methods = [method_name, 'OPRL']
    base_idx = 0

    skip_ratio_mean = {
        method_name: [0.183333333, 0.25, 0.3],
        'OPRL': [0.5, 0.333333333, 0.416666667],
    }
    skip_ratio_stderr = {
        method_name: [0.047140452, 0.081649658, 0.147196014],
        'OPRL': [0.035355339, 0.023570226, 0.131233465],
    }
    error_ratio_mean = {
        method_name: [0.2, 0.2, 0.183333333],
        'OPRL': [0.2125, 0.266666667, 0.35],
    }
    error_ratio_stderr = {
        method_name: [0.141421356, 0.147196014, 0.084983659],
        'OPRL': [0.04145781, 0.023570226, 0.227303028],
    }
    skip_mean_data = 100 - np.array([
        skip_ratio_mean[method_name],
        skip_ratio_mean['OPRL'],
    ]).T * 100.0  # (3, 2)
    skip_stderr = np.array([
        skip_ratio_stderr[method_name],
        skip_ratio_stderr['OPRL'],
    ]).T * 100.0
    error_mean_data = 100 - np.array([
        error_ratio_mean[method_name],
        error_ratio_mean['OPRL'],
    ]).T * 100.0  # (3, 2)
    error_stderr = np.array([
        error_ratio_stderr[method_name],
        error_ratio_stderr['OPRL'],
    ]).T * 100.0


    # 绘图
    fig, ax = plt.subplots(figsize=(4.5, 3))

    # 设置条形图的位置和宽度
    width = 0.4
    true_width = 0.3
    x = np.arange(len(tasks))  # x轴的位置

    # 绘制条形图
    # rects1 = ax.bar(x, skip_mean_data[:, 0], true_width, label=method_name , 
    #                 color='mistyrose', edgecolor='indianred', linewidth=1.8)
    # rects2 = ax.bar(x + width, skip_mean_data[:, 1], true_width, label='OPRL', 
    #                 color='floralwhite', edgecolor='peru', linewidth=1.8)
    # ax.errorbar(x, skip_mean_data[:, 0], yerr=skip_stderr[:, 0], fmt='none', color='indianred', capsize=5, linewidth=1.4)
    # ax.errorbar(x + width, skip_mean_data[:, 1], yerr=skip_stderr[:, 1], fmt='none', color='peru', capsize=5, linewidth=1.4)
    # ax.set_ylabel('Query clarity ratio (%)', fontsize=16)


    rects1 = ax.bar(x, error_mean_data[:, 0], true_width, label=method_name,
                    color='honeydew', edgecolor='mediumseagreen', linewidth=1.8)
    rects2 = ax.bar(x + width, error_mean_data[:, 1], true_width, label='OPRL',
                    color='lavender', edgecolor='mediumpurple', linewidth=1.8)
    ax.errorbar(x, error_mean_data[:, 0], yerr=error_stderr[:, 0], fmt='none', color='mediumseagreen', capsize=5, linewidth=1.4)
    ax.errorbar(x + width, error_mean_data[:, 1], yerr=error_stderr[:, 1], fmt='none', color='mediumpurple', capsize=5, linewidth=1.4)
    ax.set_ylabel('Accuracy (%)', fontsize=16)

    # 添加标签和标题
    ax.set_xticks(x + width / 2)  # 设置 x 轴标签的位置
    # ax.set_xticks(x)
    ax.set_xticklabels(tasks, rotation=0, ha='center')
    ax.set_ylim(20, 100)
    ax.set_yticks([20, 50, 75, 100])
    ax.yaxis.grid(True, color='lightgray', linestyle='-', linewidth=1)
    ax.set_axisbelow(True)

    
    lines, labels = fig.axes[-1].get_legend_handles_labels()
    fig.legend(lines, labels, bbox_to_anchor=(0.5, 1.02), loc='center',
               ncol=4, prop={'size': 16}, frameon=False)

    # 显示图形
    plt.tight_layout()
    # plt.show()
    plt.savefig('./human_exp/human_like_pbhim.png', bbox_inches='tight', pad_inches=0.2)
    plt.savefig('./human_exp/human_like_pbhim.pdf', bbox_inches='tight', pad_inches=0.2)
    plt.close()

human get confused (clarify)

import matplotlib.pyplot as plt
import numpy as np

color_list = [
    ['mistyrose', 'indianred'],
    ['floralwhite', 'peru'],
    ['aliceblue', 'dodgerblue'],
    ['honeydew', 'mediumseagreen'],
    ['lavender', 'mediumpurple'],
    ['lavenderblush', 'violet'],
]


if __name__ == '__main__':
    # 改字体大小
    plt.rcParams.update({'font.size': 14})
    # 不要使用 type 3 字体
    plt.rcParams.update({'pdf.fonttype': 42})

    # 数据
    tasks = ['dial-turn', 'hammer', 'walker-walk']
    skip_ratio_mean = {  # 10, 100, 300
        'dial-turn': [71.67, 73.33, 91.67],
        'hammer': [58.33, 91.67, 98.33],
        'walker-walk': [45.00, 93.33, 100.00],
    }
    skip_ratio_stderr = {
        'dial-turn': [14.34, 2.36, 2.36],
        'hammer': [2.36, 4.71, 2.36],
        'walker-walk': [10.80, 2.36, 0.00],
    }
    error_ratio_mean = {
        'dial-turn': [72.70, 77.56, 96.39],
        'hammer': [83.08, 89.27, 100.00],
        'walker-walk': [100.00, 91.13, 100.00],
    }
    error_ratio_stderr = {
        'dial-turn': [9.89, 5.52, 2.55],
        'hammer': [6.50, 4.05, 0.00],
        'walker-walk': [0.00, 2.34, 0.00],
    }

    # 生成条形图的 x 轴位置
    x = np.arange(len(tasks))  # 任务数目
    width = 0.26  # 条形宽度
    true_width = 0.2

    # 创建图形
    fig, ax = plt.subplots(figsize=(6.4, 2.2))

    # 绘制条形图
    rects1 = ax.bar(x - width, [skip_ratio_mean[ta][2] for ta in tasks], true_width, label='Large Return Difference (RD)', 
                    color=color_list[0][0], edgecolor=color_list[0][1], linewidth=1.8)  # hatch='//'
    rects2 = ax.bar(x, [skip_ratio_mean[ta][1] for ta in tasks], true_width, label='Medium RD', 
                    color=color_list[1][0], edgecolor=color_list[1][1], linewidth=1.8)  # hatch='\\'
    rects3 = ax.bar(x + width, [skip_ratio_mean[ta][0] for ta in tasks], true_width, label='Small RD', 
                    color=color_list[2][0], edgecolor=color_list[2][1], linewidth=1.8)  # hatch='--'
    ax.errorbar(x - width, [skip_ratio_mean[ta][2] for ta in tasks], yerr=[skip_ratio_stderr[ta][2] for ta in tasks], 
                fmt='none', color=color_list[0][1], capsize=5, linewidth=1.4)
    ax.errorbar(x, [skip_ratio_mean[ta][1] for ta in tasks], yerr=[skip_ratio_stderr[ta][1] for ta in tasks], 
                fmt='none', color=color_list[1][1], capsize=5, linewidth=1.4)
    ax.errorbar(x + width, [skip_ratio_mean[ta][0] for ta in tasks], yerr=[skip_ratio_stderr[ta][0] for ta in tasks], 
                fmt='none', color=color_list[2][1], capsize=5, linewidth=1.4)
    ax.set_ylim(20, 100)
    ax.set_yticks([20, 50, 75, 100])
    ax.set_ylabel('Query clarity ratio (%)', fontsize=14)


    # rects1 = ax.bar(x - width, [error_ratio_mean[ta][2] for ta in tasks], true_width, label='Large Return Difference (RD)', 
    #             color=color_list[3][0], edgecolor=color_list[3][1], linewidth=1.8)  # hatch='//'
    # rects2 = ax.bar(x, [error_ratio_mean[ta][1] for ta in tasks], true_width, label='Medium RD',
    #             color=color_list[4][0], edgecolor=color_list[4][1], linewidth=1.8)  # hatch='\\'
    # rects3 = ax.bar(x + width, [error_ratio_mean[ta][0] for ta in tasks], true_width, label='Small RD',
    #             color=color_list[5][0], edgecolor=color_list[5][1], linewidth=1.8)  # hatch='--'
    # ax.errorbar(x - width, [error_ratio_mean[ta][2] for ta in tasks], yerr=[error_ratio_stderr[ta][2] for ta in tasks],
    #             fmt='none', color=color_list[3][1], capsize=5, linewidth=1.4)
    # ax.errorbar(x, [error_ratio_mean[ta][1] for ta in tasks], yerr=[error_ratio_stderr[ta][1] for ta in tasks],
    #             fmt='none', color=color_list[4][1], capsize=5, linewidth=1.4)
    # ax.errorbar(x + width, [error_ratio_mean[ta][0] for ta in tasks], yerr=[error_ratio_stderr[ta][0] for ta in tasks],
    #             fmt='none', color=color_list[5][1], capsize=5, linewidth=1.4)
    # ax.set_ylim(60, 100)
    # ax.set_yticks([60, 80, 100])
    # ax.set_ylabel('Accuracy (%)', fontsize=16)

    ax.set_xticks(x)
    ax.set_xticklabels(tasks, ha='center', fontsize=16)  # , fontweight='bold'
    ax.yaxis.grid(True, color='lightgray', linestyle='-', linewidth=1)
    ax.set_axisbelow(True)


    # ax.legend(fontsize=12)
    lines, labels = fig.axes[-1].get_legend_handles_labels()
    labels = labels[:2] + [''] + labels[2:]
    lines = lines[:2] + [plt.Line2D([0], [0], color='white', lw=0)] + lines[2:]
    fig.legend(lines, labels, bbox_to_anchor=(0.5, 1.02), loc='center',
               ncol=2, prop={'size': 14}, frameon=False)

    # 显示图形
    plt.tight_layout()
    # plt.show()
    plt.savefig('./human_exp/human_get_confused.png', bbox_inches='tight', pad_inches=0.2)
    plt.savefig('./human_exp/human_get_confused.pdf', bbox_inches='tight', pad_inches=0.2)
    plt.close()


posted @ 2025-12-08 23:06  MoonOut  阅读(2)  评论(0)    收藏  举报