Seaborn 是一个 Python 数据可视化库,它基于 Matplotlib 构建,提供了更高级的图形绘制功能。以下是一些关于 Seaborn 的基本教程内容。
安装 Seaborn
pip install seaborn
基本使用
Seaborn 提供了多种类型的图表,包括散点图、条形图、箱线图等。以下是一个简单的散点图示例:
import seaborn as sns
import matplotlib.pyplot as plt
# 加载数据
tips = sns.load_dataset('tips')
# 绘制散点图
sns.scatterplot(x='total_bill', y='tip', data=tips)
# 显示图表
plt.show()
数据分布图
Seaborn 还可以绘制数据分布图,例如直方图和密度图。
# 绘制直方图
sns.histplot(tips['total_bill'], bins=30)
# 绘制密度图
sns.kdeplot(tips['total_bill'])
# 显示图表
plt.show()
关联分析
Seaborn 提供了多种关联分析图表,例如点图和热图。
# 绘制点图
sns.pointplot(x='day', y='total_bill', data=tips)
# 绘制热图
sns.heatmap(tips.corr(), annot=True)
# 显示图表
plt.show()
Seaborn 与 Pandas 的结合
Seaborn 与 Pandas 结合可以更方便地进行数据可视化。
import pandas as pd
# 创建 DataFrame
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [5, 4, 3, 2, 1]
})
# 使用 Seaborn 绘制散点图
sns.scatterplot(x='A', y='B', data=df)
# 显示图表
plt.show()
扩展阅读
更多关于 Seaborn 的教程和示例,请访问Seaborn 教程。
<center><img src="https://cloud-image.ullrai.com/q/Seaborn_Example/" alt="Seaborn Example"/></center>