您好,欢迎来到欧得旅游网。
搜索
您的当前位置:首页python 柱状图 标签

python 柱状图 标签

来源:欧得旅游网

效果图

代码


def plot_bar(sorted_barcode_count_list, top=200, title='Title', out_file="bar.png"):
    '''
    {AAA: 2, TTT: 3}
    '''
    barcode_list = [i[0] for i in sorted_barcode_count_list]
    count_list = [i[1] for i in sorted_barcode_count_list]
    sum_count = sum(count_list)
    x = barcode_list[:top]
    y = count_list[:top]
    
    plt.figure(figsize=(20, 10))
    
    plt.grid(ls="--", alpha=0.5)
    plt.bar(x, y, color='blue', alpha=0.6)
    
    plt.scatter(range(len(x)), y, color='red', s=3)
    # 调整文本位置
    # text_objects = [plt.text(i, j+0.2, str(round(j / sum_count, 5))+'%', ha='center', va='bottom', size=4, rotation=60) 
    #                 for i, j in zip(x, y)]
    # adjust_text(text_objects)
    
    for i, j in zip(x, y):
        plt.text(i, j+0.6, str(j) + '(' + str(round(j / sum_count *100, 3))+'%)', 
                 ha='center', va='bottom', size=4, rotation=60)
    plt.title(title)
    plt.xlabel("Barcode", labelpad=50)
    plt.ylabel("Count", labelpad=50)
    plt.xticks(rotation=70, size=7)
    plt.tight_layout()
    plt.savefig(out_file, dpi=500)   # , bbox_inches='tight') 
    print(f"图像已保存为 {out_file}")


因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- ovod.cn 版权所有 湘ICP备2023023988号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务