如何在 Seaborn Barplot 上显示值?

您所在的位置:网站首页 如何在条形图上显示数据值 如何在 Seaborn Barplot 上显示值?

如何在 Seaborn Barplot 上显示值?

2024-07-12 09:04| 来源: 网络整理| 查看: 265

在本文中,我们将了解如何使用 Python 在 Seaborn Barplot 上显示值。

Seaborn 是一个构建在 matplotlib 之上的数据可视化包,它使 seaborn 在不同的图表中具有多种自定义功能。通常,条形图将分类数据总结为矩形条,其高度与相应条的值成比例。 Seaborn 库提供了一个名为 Barplot() 的方法,该方法加载了 10 多个参数,可以绘制满足大多数要求的条形图。在这篇博客中,让我们讨论如何在 seaborn 条形图上显示条形值。

语法:seaborn.barplot(data, x=None, y=None, hue=None, data=None, order=None, orient=None, color=None, palette=None, 饱和度=0.75,errwidth)

参数:

data - 指定用于条形图的dataframe x, y – 指定要沿 x 轴和 y 轴使用的数据。 (长格式数据) order – 指定分类值的绘制顺序 orient - ‘v’指定垂直方向,‘h’指定水平方向 color - 指定所有元素的颜色渐变 调色板 - 必须用于不同级别的颜色列表 errwidth – 指定错误栏宽度 示例1:

使用的数据集:提示

在带有 bar 的 seaborn barplot 中,可以使用 sns.barplot() 函数和 sns.barplot() 返回的子方法容器绘制值。导入 pandas、numpy 和 seaborn 包。使用 pandas read_csv 函数读取数据集。现在,在两列之间创建一个条形图,在这里,让我们选择 x 轴是时间,y 轴作为提示。这将返回一个传统的条形图。该绘图对象存储在一个变量中。 plot 对象有一个称为容器的方法,可以列出每个条的属性。遍历容器对象的列表项并将每个项传递给 bar_label 函数。这将提取并显示条形图中的条形值。

Python3实现

# import the necessary python packages import pandas as pd import seaborn as sns import numpy as np  # read the dataset using pandas read_csv # function data = pd.read_csv(r"path to ips.csv")  data.head()  # create a bar plot by specifying # x and y axis and the data to be used. ax = sns.barplot(x='time', y='tip',                  hue='sex', data=data,                  errwidth=0)  # sns.barplot method will return a list of # sub methods use containers method to access # the text label of each bar by passing it # through the ax.bar_label function use for # loop to iterate through the list of # labels and assign each bar to a different # label. for i in ax.containers:     ax.bar_label(i,)

输出:

带有条形值的条形图

示例 2:

与上述方法不同,可以使用 sns.barplot() 函数和 sns.barplot() 返回的相同子方法容器为分组条形图绘制带有条形值的 seaborn 条形图。导入 pandas、NumPy 和 seaborn 包。使用 pandas read_csv 函数读取数据集。现在,从此处的分组dataframe中创建一个条形图,让我们选择 x 轴作为天,y 轴作为总账单。这将再次返回传统的条形图。该绘图对象存储在一个变量中。 plot 对象有一个称为容器的方法,可以列出每个条的属性。现在,将容器对象传递给 bar_label 函数。这将提取并显示条形图中的条形值。

Python3实现

# import the necessary python packages import pandas as pd import seaborn as sns import numpy as np  # read the dataset using pandas read_csv # function data = pd.read_csv(r"path to ips.csv")  # group the multi level categorical variables # and reset_ the index to flatten the index groupedvalues = data.groupby('day').sum().reset_index()  # use sns barplot to plot bar plot # between days and tip value ax = sns.barplot(x='day', y='tip',                  data=groupedvalues,                  errwidth=0)  # now simply assign the bar values to # each bar by passing containers method # to bar_label function ax.bar_label(ax.containers[0])

输出:

带有条形值的条形图 - 分组dataframe

示例 3:

如上所述,导入必要的包并使用 pandas read_csv() 函数读取数据。 Sum 基于 day 列聚合数据。现在,使用您希望的颜色定义一个调色板列表。此调色板列表的长度必须对应于分组数据帧的长度。在这里,我们有兴趣在 day 和 total_bill 列之间绘制条形图。因此,argsort() 总帐单列将对列进行排序并返回已排序列的索引。现在,将 x、y 列传递给 barplot 函数必须使用的数据。除此之外,将调色板对象转换为 numpy 数组并根据 argsort() 值重新排列数组。传递所有这些参数将返回一个箱线图。目的。现在使用 iterrows() 遍历分组数据帧的每一行,并使用 barplot 对象的 text 函数将行的所需值分配为 bar 值。

Python3实现

# import the necessary python packages import pandas as pd import seaborn as sns import numpy as np  # read the dataset using pandas read_csv # function data = pd.read_csv(r"path to ips.csv")  # group the multilevel categorical # values and flatten the index groupedvalues = data.groupby('day').sum().reset_index()  # define the color palette of different colors pal = sns.color_palette("Greens_d", len(groupedvalues)) # use argsort method rank = groupedvalues["total_bill"].argsort()  # use dataframe grouped by days to plot a # bar chart between days and total bill ax = sns.barplot(x='day', y='total_bill',                  data=groupedvalues,                  palette=np.array(pal[::-1])[rank])  # now use a for loop to iterate through # each row of the grouped dataframe # assign bar value  to each row for index, row in groupedvalues.iterrows():     ax.text(row.name, row.tip, round(row.total_bill, 2),             color='white', ha='center')

输出:

带有条形值的条形图 - 分组dataframe



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3