折线图
import pandas as pd
import seaborn as sns
flights=pd.read_csv('C:\\work\\flights.csv')
flights.head()
点击查看详情

# 设置x轴和y轴
may_flights =flights.query("month == 'May'")
sns.lineplot(data=may_flights, x="year", y="passengers")
点击查看详情

# 分类
sns.lineplot(data=flights, x="year", y="passengers", hue="month")
点击查看详情

# 样式
sns.lineplot(data=flights, x="year", y="passengers", hue="month", style="month")
点击查看详情

# 图形方向
sns.lineplot(data=flights, x="passengers", y="year", orient="y")
点击查看详情

柱形图
penguins=pd.read_csv('C:\\work\\penguins.csv')
penguins.head()
点击查看详情

# 选择1列
sns.histplot(data=penguins, x="flipper_length_mm")
点击查看详情

# 宽度
sns.histplot(data=penguins, x="flipper_length_mm", binwidth=3)
点击查看详情

# 数量
sns.histplot(data=penguins, x="flipper_length_mm", bins=15)
点击查看详情

# 分布
sns.histplot(data=penguins, x="flipper_length_mm", bins=30, kde=True)
点击查看详情

# 不同species的flipper_length_mm分布
sns.histplot(data=penguins, x="flipper_length_mm", hue="species")
点击查看详情

# 不同island的body_mass_g
sns.barplot(data=penguins, x="island", y="body_mass_g")
点击查看详情

sns.barplot(data=penguins,x="island",y="body_mass_g",hue="species")
点击查看详情

# 累计
sns.barplot(flights,x="year",y="passengers",estimator="sum",errorbar=None)
点击查看详情

散点图
tips=pd.read_csv('C:\\work\\tips.csv')
tips.head()
点击查看详情

# 指定x轴和y轴
sns.scatterplot(data=tips,x="total_bill",y="tip")
点击查看详情

# 加条件
sns.scatterplot(data=tips,x="total_bill",y="tip",hue="smoker")
点击查看详情

# size指定大小
sns.scatterplot(data=tips,x="total_bill",y="tip",hue="day",style="time",size="size")
点击查看详情

# 相关性
import numpy as np
np.corrcoef(tips['total_bill'],tips['tip'])
点击查看详情
array([[1. , 0.67573411],
[0.67573411, 1. ]])
# 抽样100条
sample_data = data.sample(n=100)
# 抽样2%
sample_data = data.sample(frac=0.02)
len(sample_data)