安装 Seaborn 非常简单:
<code>pip install seaborn</code>
在使用时只需要导入即可:
<code>import seaborn as sns</code>
Seaborn 提供了一些内置的数据集,这里我们使用了 Iris 数据集。
<code>data = sns.load_dataset('iris')</code>
我们先来看一下数据:
<code>print(data[10:15])</code>
1. 条形图
条形图用于表示分类变量,它显示了每个分类的平均值或其他估计值。
<code>sns.barplot(x='species', y='petal_length', hue='species', data=data)</code>
2. 散点图
散点图显示了数据点的分布情况。
<code>sns.scatterplot(x='petal_length', y='sepal_length', hue='species', style='species', s=90, data=data)</code>
3. 直方图
直方图通常用于表示单个变量的分布情况,也可以用于比较多个变量的分布情况。
<code>sns.histplot(x='sepal_length', kde=True, data=data)</code>
<code>sns.histplot(x='sepal_length', kde=True, hue='species', data=data)</code>
4. 线形图
线形图用于显示不同变量之间的关系。
<code>sns.lineplot(x='petal_length', y='petal_width', data=data)</code>
5. 小提琴图
小提琴图显示了数据的密度分布情况。
<code>sns.violinplot(x='species', y='petal_length', data=data, hue='species')</code>
6. 箱线图
箱线图显示了数据的分布情况,包括中位数、四分位数等。
<code>sns.boxplot(x='species', y='sepal_length', data=data, hue='species')</code>
7. 热图
热图用颜色表示数据的值,常用于显示数据的相关性。
<code>heat_corr = data.corr() sns.heatmap(heat_corr, annot=True)</code>
8. 点图
点图用于显示一组数据的集中趋势。
<code>sns.pointplot(x='species', y='petal_length', data=data, markers='^', color='g')</code>
9. 密度图
密度图显示数据的概率密度分布。
<code>sns.kdeplot(x='petal_length', data=data, hue='species', multiple='stack')</code>
<code>sns.kdeplot(x='petal_length', y='sepal_length', data=data, hue='species')</code>
10. 计数图
计数图显示分类变量的计数。
<code>sns.countplot(x='species', data=data)</code>
11. 分簇散点图
分簇散点图用于显示数据点的分布情况,防止重叠。
<code>sns.swarmplot(x='sepal_width', y='species', data=data, hue='species', dodge=True, orient='h', size=8)</code>
12. 配对图
配对图显示了数据集中变量之间的成对关系。
<code>sns.pairplot(data=data, hue='species')</code>
13. Facet Grid
FacetGrid 可以根据一个或多个分类变量创建一个图表网格。
<code>g = sns.FacetGrid(data, col='species', height=4, hue='species') g.map(sns.histplot, 'petal_length')</code>
14. 联合分布图
联合分布图显示两个变量之间的关系。
<code>sns.jointplot(x='sepal_length', y='sepal_width', data=data, palette='Set2', hue='species')</code>
15. 分类图
分类图用于显示分类变量与连续变量之间的关系。
<code>sns.catplot(data=data, x='petal_length', y='species', kind='violin', color='.9', inner=None) sns.swarmplot(data=data, x='petal_length', y='species', size=3)</code>
总结
Seaborn 是一款强大且易用的数据可视化工具,为探索和交流数据提供了很多便利。它与其他 Python 数据分析库(如 Pandas)的集成使得数据探索和可视化变得更加简单。
© 版权声明
THE END
暂无评论内容