您的位置:宽带测速网 > 编程知识 > Seaborn的countplot()函数怎么使用

Seaborn的countplot()函数怎么使用

2025-07-04 09:14来源:互联网 [ ]

Seaborn的countplot()函数可以用来绘制分类变量的计数柱状图。使用方法如下:

    导入Seaborn库和数据集:
import seaborn as snsimport matplotlib.pyplot as plt# 导入数据集data = sns.load_dataset("tips")
    调用countplot()函数绘制计数柱状图:
sns.countplot(x='day', data=data)plt.show()

在上面的例子中,我们以"day"列作为x轴,绘制了关于"day"列的计数柱状图。您也可以指定其他参数,比如hue参数来对另一个变量进行分组显示:

sns.countplot(x='day', hue='sex', data=data)plt.show()

这样就可以绘制出关于"day"列和"sex"列的计数柱状图,以不同性别分组显示。您可以根据需要灵活调整参数来绘制不同类型的计数柱状图。