Seaborn 中的猫图

Manav Narula 2021年10月2日
Seaborn 中的猫图

seaborn 模块中有许多类型的图可用。但是,在处理分类值时,我们可能需要一种常量方法来绘制数据,因为不同的绘图函数的工作方式不同。这种方法与数据不一致。该模块的 catplot() 函数用于有效地处理分类数据。此函数旨在改进最新版本的 seaborn 模块中的 factorplot() 函数。

它使我们能够有效地处理分类值,并且我们可以将数据绘制成由 kind 参数指定的八种不同类型的图形。catplot() 函数返回一个 FacetGrid() 类型的对象,因此可以有效地利用它为同一图形上的多个特征绘制图形。

使用 catplot() 函数很简单。请参考以下代码。

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame(
    {
        "Product": [1, 1, 2, 3, 1, 2, 2, 3, 3, 3, 3, 1, 2, 2, 1],
        "Max_Price": [78, 79, 55, 26, 80, 54, 50, 24, 25, 22, 23, 80, 53, 54, 77],
    }
)

sns.catplot(data=df, x="Product", y="Max_Price", kind="strip")

seaborn 中的猫图

请注意,我们在 x 轴上绘制了产品类别,在 y 轴上绘制了所需的特征 Max_Price。这种方法对于研究具有类别的不同特征非常方便。

在上面的代码中,我们通过将 kind 参数指定为 strip 绘制了一个 stripplot()。我们可以将其更改为任何需要的图,例如 barplot()barboxplot()box,等等。检查下图以了解有关使用此功能可能的所有不同绘图的更多信息。

seaborn 中的猫图 - catplotkind

其他参数可用于自定义最终图形。例如,我们可以分别使用 heightaspect 参数更改最终图形的大小,使用 order 参数更改绘制的类别的顺序,等等。

作者: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn