Seaborn是Python中的一个库,主要用于生成统计图形。
Seaborn是构建在matplotlib之上的数据可视化库,与Python中的pandas数据结构紧密集成。可视化是Seaborn的核心部分,可以帮助探索和理解数据。
要了解Seaborn,就必须熟悉Numpy和Matplotlib以及pandas。
Seaborn提供以下功能:
- 面向数据集的API来确定变量之间的关系。
- 线性回归曲线的自动计算和绘制。
- 它支持对多图像的高级抽象绘制。
- 可视化单变量和双变量分布。
这些只是Seaborn提供的功能的一部分,还有很多其他功能,我们可以在这里探索所有的功能。
要引入Seaborn库,使用的命令是:
importseabornassns
使用Seaborn,我们可以绘制各种各样的图形,如:
- 分布曲线
- 饼图和柱状图
- 散点图
- 配对图
- 热力图
在文章中,我们使用从Kaggle下载的谷歌Playstore数据集。
1.分布曲线
我们可以将Seaborn的分布图与Matplotlib的直方图进行比较。它们都提供非常相似的功能。这里我们画的不是直方图中的频率图,而是y轴上的近似概率密度。
我们将在代码中使用sns.distplot()来绘制分布图。
在进一步之前,首先,让我们访问我们的数据集,
importpandasaspdimportnumpyasnppstore=pd.read_csv("googleplaystore.csv") pstore.head(10)
从我们的系统访问数据集
数据集是这样的,
从Kaggle获得的谷歌播放商店数据集
现在,让我们看看如果我们绘制来自上述数据集的“Rating”列的分布图是怎样的,
#importingallthelibrariesimportnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltimportseabornassns#Createadistributionplotforratingsns.distplot(pstore.Rating) plt.show()
Rating列分布图的代码
Rating列的分布图是这样的,
在这里,曲线(KDE)显示在分布图上的是近似的概率密度曲线。
与matplotlib中的直方图类似,在分布方面,我们也可以改变类别的数量,使图更容易理解。
我们只需要在代码中加上类别的数量,
#Changethenumberofbinssns.distplot(inp1.Rating, bins=20, kde=False) plt.show()
图像是这样的,
特定类别数的分布图
在上图中,没有概率密度曲线。要移除曲线,我们只需在代码中写入' kde = False '。
我们还可以向分布图提供与matplotlib类似的容器的标题和颜色。让我们看看它的代码,
#importingallthelibrariesimportnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltimportseabornassns#Createadistributionplotforratingsns.distplot(pstore.Rating, bins=20, color="g") plt.title("Distribution of app ratings", fontsize=20, color='red') plt.show()
同一列Rating的分布图是这样的:
有标题的分布图
对Seaborn图形进行样式化
使用Seaborn的最大优势之一是,它为图形提供了广泛的默认样式选项。
这些是Seaborn提供的默认样式。
'Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10'
我们只需要编写一行代码就可以将这些样式合并到我们的图中。
#importingallthelibrariesimportnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltimportseabornassns#Addingdarkbackgroundtothegraphplt.style.use("dark_background") #Createadistributionplotforratingsns.distplot(pstore.Rating, bins=20, color="g") plt.title("Distribution of app ratings", fontsize=20, color='red') plt.show()
在将深色背景应用到我们的图表后,分布图看起来是这样的,
深色背景的分布图
2.饼图和柱状图
饼图通常用于分析数字变量在不同类别之间如何变化。
在我们使用的数据集中,我们将分析内容Rating栏中的前4个类别的执行情况。
首先,我们将对内容Rating列进行一些数据清理/挖掘,并检查其中的类别。
#importingallthelibrariesimportnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltimportseabornassns#AnalyzingtheContentRatingcolumnpstore['Content Rating'].value_counts()
类别列表是,
Rating列数
根据上面的输出,由于“只有18岁以上的成年人”和“未分级”的数量比其他的要少得多,我们将从内容分级中删除这些类别并更新数据集。
#importingallthelibrariesimportnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltimportseabornassns#Removetherowswithvalueswhicharelessrepresentedpstore=pstore[~pstore['Content Rating'].isin(["Adults only 18+","Unrated"])] #Resettingtheindexpstore.reset_index(inplace=True, drop=True) #AnalyzingtheContentRatingcolumnagainpstore['Content Rating'].value_counts()
更新后在“Rating”栏中出现的类别是:
更新数据集后的Rating计数