比较(一)利用python绘制条形图

2024-05-29 02:12
文章标签 python 比较 绘制 条形图

本文主要是介绍比较(一)利用python绘制条形图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

比较(一)利用python绘制条形图

条形图(Barplot)简介

1

条形图主要用来比较不同类别间的数据差异,一条轴表示类别,另一条则表示对应的数值度量。

快速绘制

  1. 基于seaborn

    import seaborn as sns
    import matplotlib.pyplot as plt# 导入数据
    tips = sns.load_dataset("tips")# 利用barplot函数快速绘制
    sns.barplot(x="total_bill", y="day", data=tips, estimator=sum, errorbar=None, color='#69b3a2')plt.show()
    

    2

  2. 基于matplotlib

    import matplotlib.pyplot as plt# 导入数据
    tips = sns.load_dataset("tips")
    grouped_tips = tips.groupby('day')['total_bill'].sum().reset_index()# 利用bar函数快速绘制
    plt.bar(grouped_tips.day, grouped_tips.total_bill)plt.show()
    

    3

  3. 基于pandas

    import matplotlib.pyplot as plt
    import pandas as pd# 导入数据
    tips = sns.load_dataset("tips")
    grouped_tips = tips.groupby('day')['total_bill'].sum().reset_index()# 利用plot.bar函数快速绘制
    grouped_tips.plot.bar(x='day', y='total_bill', rot=0)plt.show()
    

    4

定制多样化的条形图

自定义条形图一般是结合使用场景对相关参数进行修改,并辅以其他的绘图知识。参数信息可以通过官网进行查看,其他的绘图知识则更多来源于实战经验,大家不妨将接下来的绘图作为一种学习经验,以便于日后总结。

通过seaborn绘制多样化的条形图

seaborn主要利用barplot绘制条形图,可以通过seaborn.barplot了解更多用法

  1. 修改参数

    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as npsns.set(font='SimHei', font_scale=0.8, style="darkgrid") # 解决Seaborn中文显示问题# 导入数据
    tips = sns.load_dataset("tips")# 构造子图
    fig, ax = plt.subplots(2,2,constrained_layout=True, figsize=(8, 8))# 修改方向-垂直
    ax_sub = sns.barplot(y="total_bill", x="day", data=tips, estimator=sum, errorbar=None, color='#69b3a2',ax=ax[0][0])
    ax_sub.set_title('垂直条形图')# 自定义排序
    ax_sub = sns.barplot(y="total_bill", x="day", data=tips, estimator=sum, errorbar=None, color='#69b3a2',order=["Fri","Thur","Sat","Sun"],ax=ax[0][1])
    ax_sub.set_title('自定义排序')# 数值排序
    df = tips.groupby('day')['total_bill'].sum().sort_values(ascending=False).reset_index()
    ax_sub = sns.barplot(y="day", x="total_bill", data=df, errorbar=None, color='#69b3a2',order=df['day'],ax=ax[1][0])
    ax_sub.set_title('数值排序')# 添加误差线
    ax_sub = sns.barplot(x="day", y="total_bill", data=tips, estimator=np.mean, errorbar=('ci', 85), capsize=.2, color='lightblue',ax=ax[1][1])
    ax_sub.set_title('添加误差线')plt.show()
    

    5

  2. 分组条形图

    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as npsns.set(style="darkgrid")# 导入数据
    tips = sns.load_dataset("tips")fig, ax = plt.subplots(figsize=(4, 4))# 分组条形图
    colors = ["#69b3a2", "#4374B3"]
    sns.barplot(x="day", y="total_bill", hue="smoker", data=tips, errorbar=None, palette=colors)plt.show()# 分组/子分组条形图
    sns.catplot(x="sex", y="total_bill", hue="smoker", col="day", data=tips, kind="bar", height=4, aspect=.7)plt.show()
    

    6

  3. 引申-数量堆积条形图

    import seaborn as sns
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.patches as mpatchessns.set(style="darkgrid")# 导入数据
    tips = sns.load_dataset("tips")
    df = tips.groupby(['day', 'smoker'])['total_bill'].sum().reset_index()
    smoker_df = df[df['smoker']=='Yes']
    non_smoker_df = df[df['smoker']=='No']# 布局
    plt.figure(figsize=(6, 4))# 非吸烟者的条形图
    bar1 = sns.barplot(x='day', y='total_bill', data=non_smoker_df, color='lightblue')
    # 吸烟者的条形图,底部开始位置设置为非吸烟者的total_bill值(即吸烟者条形图在上面)
    bar2 = sns.barplot(x='day', y='total_bill', bottom=non_smoker_df['total_bill'], data=smoker_df, color='darkblue')# 图例
    top_bar = mpatches.Patch(color='darkblue', label='smoker = Yes')
    bottom_bar = mpatches.Patch(color='lightblue', label='smoker = No')
    plt.legend(handles=[top_bar, bottom_bar])plt.show()
    

    7

  4. 引申-百分比堆积条形图

    import seaborn as sns
    import matplotlib.pyplot as plt
    import pandas as pd# 导入数据
    tips = sns.load_dataset("tips")# 计算百分比
    day_total_bill = tips.groupby('day')['total_bill'].sum() # 每日数据
    group_total_bill = tips.groupby(['day', 'smoker'])['total_bill'].sum().reset_index() # 每日每组数据
    group_total_bill['percent'] = group_total_bill.apply(lambda row: row['total_bill'] / day_total_bill[row['day']] * 100, axis=1)# 将数据分成smoker和non-smoker两份,方便我们绘制两个条形图
    smoker_df = group_total_bill[group_total_bill['smoker'] == 'Yes']
    non_smoker_df = group_total_bill[group_total_bill['smoker'] == 'No']# 布局
    plt.figure(figsize=(6, 4))# 非吸烟者的条形图
    bar1 = sns.barplot(x='day', y='percent', data=non_smoker_df, color='lightblue')
    # 吸烟者的条形图,底部开始位置设置为非吸烟者的total_bill值(即吸烟者条形图在上面)
    bar2 = sns.barplot(x='day', y='percent', bottom=non_smoker_df['percent'], data=smoker_df, color='darkblue')# 图例
    top_bar = mpatches.Patch(color='darkblue', label='smoker = Yes')
    bottom_bar = mpatches.Patch(color='lightblue', label='smoker = No')
    plt.legend(handles=[top_bar, bottom_bar])plt.show()
    

    8

通过seaborn绘制多样化的条形图

seaborn主要利用barh绘制条形图,可以通过matplotlib.pyplot.barh了解更多用法

  1. 修改参数

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import numpy as np 
    import pandas as pdmpl.rcParams.update(mpl.rcParamsDefault) # 恢复默认的matplotlib样式
    plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签# 自定义数据
    height = [3, 12, 5, 18, 45]
    bars = ('A', 'B', 'C', 'D', 'E')
    y_pos = np.arange(len(bars))
    x_pos = np.arange(len(bars))# 初始化布局
    fig = plt.figure(figsize=(8,8))# 水平方向-水平条形图
    plt.subplot(3, 3, 1) 
    plt.barh(y_pos, height)
    plt.yticks(y_pos, bars)
    plt.title('水平条形图')# 指定顺序
    height_order, bars_order = zip(*sorted(zip(height, bars), reverse=False)) # 自定义顺序plt.subplot(3, 3, 2) 
    plt.barh(y_pos, height_order)
    plt.yticks(y_pos, bars_order)
    plt.title('指定顺序')# 自定义颜色
    plt.subplot(3, 3, 3) 
    plt.bar(x_pos, height, color=['black', 'red', 'green', 'blue', 'cyan'])
    plt.xticks(x_pos, bars)
    plt.title('自定义颜色')# 自定义颜色-边框颜色
    plt.subplot(3, 3, 4) 
    plt.bar(x_pos, height, color=(0.1, 0.1, 0.1, 0.1),  edgecolor='blue')
    plt.xticks(x_pos, bars)
    plt.title('自定义边框颜色')# 控制距离
    width = [0.1,0.2,3,1.5,0.3]
    x_pos_width = [0,0.3,2,4.5,5.5]plt.subplot(3, 3, 5) 
    plt.bar(x_pos_width, height, width=width)
    plt.xticks(x_pos_width, bars)
    plt.title('控制距离')# 控制宽度
    x_pos_space = [0,1,5,8,9]plt.subplot(3, 3, 6) 
    plt.bar(x_pos_space, height)
    plt.xticks(x_pos_space, bars)
    plt.title('控制宽度')# 自定义布局
    plt.subplot(3, 3, 7) 
    plt.bar(x_pos, height)
    plt.xticks(x_pos, bars, color='orange', rotation=90) # 自定义x刻度名称颜色,自定义旋转
    plt.xlabel('category', fontweight='bold', color = 'orange', fontsize='18') # 自定义x标签
    plt.yticks(color='orange') # 自定义y刻度名称颜色plt.title('自定义布局')# 添加误差线
    err = [val * 0.1 for val in height] # 计算误差(这里假设误差为height的10%)plt.subplot(3, 3, 8) 
    plt.bar(x_pos, height, yerr=err, alpha=0.5, ecolor='black', capsize=10)
    plt.xticks(x_pos, bars)
    plt.title('添加误差线')# 增加数值文本信息
    plt.subplot(3, 3, 9) 
    ax = plt.bar(x_pos, height)
    for bar in ax:yval = bar.get_height()plt.text(bar.get_x() + bar.get_width()/2.0, yval, int(yval), va='bottom') # va参数代表垂直对齐方式
    plt.xticks(x_pos, bars)
    plt.title('增加数值文本信息')fig.tight_layout() # 自动调整间距
    plt.show()
    

    9

  2. 分组条形图

    import numpy as np
    import matplotlib.pyplot as plt# 宽度设置
    barWidth = 0.25# 自定义数据
    bars1 = [12, 30, 1, 8, 22]
    bars2 = [28, 6, 16, 5, 10]
    bars3 = [29, 3, 24, 25, 17]# x位置
    r1 = np.arange(len(bars1))
    r2 = [x + barWidth for x in r1]
    r3 = [x + barWidth for x in r2]# 绘制分组条形图
    plt.bar(r1, bars1, color='#7f6d5f', width=barWidth, edgecolor='white', label='g1')
    plt.bar(r2, bars2, color='#557f2d', width=barWidth, edgecolor='white', label='g2')
    plt.bar(r3, bars3, color='#2d7f5e', width=barWidth, edgecolor='white', label='g3')# 轴标签、图例
    plt.xlabel('group', fontweight='bold')
    plt.xticks([r + barWidth for r in range(len(bars1))], ['A', 'B', 'C', 'D', 'E'])
    plt.legend()plt.show()
    

    10

  3. 数量堆积条形图

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd# 自定义数据
    bars1 = [12, 28, 1, 8, 22]
    bars2 = [28, 7, 16, 4, 10]
    bars3 = [25, 3, 23, 25, 17]# bars1 + bars2的高度
    bars = np.add(bars1, bars2).tolist()# x位置
    r = [0,1,2,3,4]# bar名称、宽度
    names = ['A','B','C','D','E']
    barWidth = 1# 底部bar
    plt.bar(r, bars1, color='#7f6d5f', edgecolor='white', width=barWidth, label="g1")
    # 中间bar
    plt.bar(r, bars2, bottom=bars1, color='#557f2d', edgecolor='white', width=barWidth, label="g2")
    # 顶部bar
    plt.bar(r, bars3, bottom=bars, color='#2d7f5e', edgecolor='white', width=barWidth, label="g3")# x轴设置、图例
    plt.xticks(r, names, fontweight='bold')
    plt.xlabel("group")
    plt.legend()plt.show()
    

    11

  4. 百分比堆积条形图

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd# 自定义数据
    r = [0,1,2,3,4] # x位置
    raw_data = {'greenBars': [20, 1.5, 7, 10, 5], 'orangeBars': [5, 15, 5, 10, 15],'blueBars': [2, 15, 18, 5, 10]}
    df = pd.DataFrame(raw_data)# 转为百分比
    totals = [i+j+k for i,j,k in zip(df['greenBars'], df['orangeBars'], df['blueBars'])]
    greenBars = [i / j * 100 for i,j in zip(df['greenBars'], totals)]
    orangeBars = [i / j * 100 for i,j in zip(df['orangeBars'], totals)]
    blueBars = [i / j * 100 for i,j in zip(df['blueBars'], totals)]# bar名称、宽度
    barWidth = 0.85
    names = ('A','B','C','D','E')# 底部bar
    plt.bar(r, greenBars, color='#b5ffb9', edgecolor='white', width=barWidth, label="g1")
    # 中间bar
    plt.bar(r, orangeBars, bottom=greenBars, color='#f9bc86', edgecolor='white', width=barWidth, label="g2")
    # 顶部bar
    plt.bar(r, blueBars, bottom=[i+j for i,j in zip(greenBars, orangeBars)], color='#a3acff', edgecolor='white', width=barWidth, label="g3")# x轴、图例
    plt.xticks(r, names)
    plt.xlabel("group")
    plt.legend()plt.show()
    

    12

通过pandas绘制多样化的条形图

pandas主要利用barh绘制条形图,可以通过pandas.DataFrame.plot.barh了解更多用法

  1. 修改参数

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import numpy as np 
    import pandas as pdplt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签# 自定义数据
    category = ['Group1']*30 + ['Group2']*50 + ['Group3']*20
    df = pd.DataFrame({'category': category})
    values = df['category'].value_counts()# 初始化布局
    fig = plt.figure(figsize=(8,4))# 水平方向-水平条形图
    plt.subplot(1, 2, 1) 
    values.plot.barh(grid=True)
    plt.title('水平条形图')# 自定义顺序、颜色
    # 指定顺序
    desired_order = ['Group1', 'Group2', 'Group3']
    values_order = values.reindex(desired_order)
    # 指定颜色
    colors = ['#69b3a2', '#cb1dd1', 'palegreen']plt.subplot(1, 2, 2) 
    values.plot.bar(color=colors,grid=True, )  
    plt.title('自定义顺序、颜色')fig.tight_layout() # 自动调整间距
    plt.show()
    

    13

  2. 分组条形图

    import pandas as pd
    import matplotlib.pyplot as plt# 自定义数据
    data = {"Product": ["Product A", "Product A", "Product A", "Product B", "Product B", "Product B"],"Segment": ["Segment 1", "Segment 2", "Segment 3", "Segment 1", "Segment 2", "Segment 3"],"Amount_sold": [100, 120, 120, 80, 160, 150]
    }df = pd.DataFrame(data)
    pivot_df = df.pivot(index='Segment',columns='Product',values='Amount_sold')# 分组条形图
    pivot_df.plot.bar(grid=True)plt.show()
    

    14

  3. 数量堆积条形图

    import pandas as pd
    import matplotlib.pyplot as plt# 自定义数据
    data = {"Product": ["Product A", "Product A", "Product A", "Product B", "Product B", "Product B"],"Segment": ["Segment 1", "Segment 2", "Segment 3", "Segment 1", "Segment 2", "Segment 3"],"Amount_sold": [100, 120, 120, 80, 160, 150]
    }df = pd.DataFrame(data)
    pivot_df = df.pivot(index='Segment',columns='Product',values='Amount_sold')# 堆积条形图
    pivot_df.plot.bar(stacked=True,grid=True)plt.show()
    

    15

  4. 百分比堆积条形图

    import pandas as pd
    import matplotlib.pyplot as plt# 自定义数据
    data = {"Product": ["Product A", "Product A", "Product A", "Product B", "Product B", "Product B"],"Segment": ["Segment 1", "Segment 2", "Segment 3", "Segment 1", "Segment 2", "Segment 3"],"Amount_sold": [100, 120, 120, 80, 160, 150]
    }df = pd.DataFrame(data)
    pivot_df = df.pivot(index='Segment',columns='Product',values='Amount_sold')
    pivot_df_percentage = pivot_df.div(pivot_df.sum(axis=1), axis=0) * 100# 百分比堆积条形图
    pivot_df_percentage.plot.bar(stacked=True,grid=True)# 图例
    plt.legend(bbox_to_anchor=(1.04, 1),loc='upper left')
    plt.show()
    

    16

总结

以上通过seaborn的barplot、matplotlib的bar和pandas的bar快速绘制条形图,并通过修改参数或者辅以其他绘图知识自定义各种各样的条形图来适应相关使用场景。

共勉~

这篇关于比较(一)利用python绘制条形图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1012349

相关文章

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Python包管理工具pip的升级指南

《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核