import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
绘制单变量分布
np.random.seed(0)
arr = np.random.randn(100)
sns.displot(arr,bins=10,kde=True,rug=True)
<seaborn.axisgrid.FacetGrid at 0x1dcb31164f0>
![png](https://img-blog.csdnimg.cn/img_convert/6a7152ae4ae22851b6ae793d973c2bb0.png)
绘制双变量分布
绘制散点图
df = pd.DataFrame({"x":np.random.randn(500),"y":np.random.randn(500)})
df.head()
| x | y |
---|
0 | 1.883151 | -1.550429 |
---|
1 | -1.347759 | 0.417319 |
---|
2 | -1.270485 | -0.944368 |
---|
3 | 0.969397 | 0.238103 |
---|
4 | -1.173123 | -1.405963 |
---|
sns.jointplot(x="x",y="y",data=df,kind="scatter",color="green",height=8,ratio=2,space=1)
<seaborn.axisgrid.JointGrid at 0x1dcb885e910>
![png](https://img-blog.csdnimg.cn/img_convert/a4273fea5f69d727357f6d3528150d30.png)
绘制二维直方图
sns.jointplot(x="x",y="y",data=df,kind="hex")
<seaborn.axisgrid.JointGrid at 0x1dcb8938160>
![png](https://img-blog.csdnimg.cn/img_convert/0cec9245af9346b4caeed4cb404a7b44.png)
绘制核密度估计度
sns.jointplot(x="x",y="y",data=df,kind="kde")
<seaborn.axisgrid.JointGrid at 0x1dcb6f31130>
![png](https://img-blog.csdnimg.cn/img_convert/439bbff33cc59aa73fa49380a2aed112.png)
sns.kdeplot(x="x",y="y",data=df,shade=True,cbar=True)
<AxesSubplot:xlabel='x', ylabel='y'>
![png](https://img-blog.csdnimg.cn/img_convert/b53d0a95beb6eacfb45bd574b60a073d.png)
绘制成对的双变量分布
dataset = sns.load_dataset("iris")
dataset.head()
| sepal_length | sepal_width | petal_length | petal_width | species |
---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
---|
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
---|
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
---|
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
---|
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
---|
sns.pairplot(dataset)
<seaborn.axisgrid.PairGrid at 0x1f1a0f3f250>
![png](https://img-blog.csdnimg.cn/img_convert/3312fc224b463b3af687691d170224e2.png)
用分类数据绘图
类别散点图
data = sns.load_dataset("tips")
data.head()
| total_bill | tip | sex | smoker | day | time | size |
---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
---|
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
---|
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
---|
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
---|
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
---|
sns.stripplot(x="day",y="total_bill",data=data,hue="time",jitter=False)
<AxesSubplot:xlabel='day', ylabel='total_bill'>
![png](https://img-blog.csdnimg.cn/img_convert/337ba2f9b96f2fb1efff3f923418d8f0.png)
sns.stripplot(x="day",y="total_bill",data=data,hue="time",jitter=True)
<AxesSubplot:xlabel='day', ylabel='total_bill'>
![png](https://img-blog.csdnimg.cn/img_convert/96ca2342c5467ce500d00e953f42d27c.png)
sns.swarmplot(x="day",y="total_bill",data=data)
<AxesSubplot:xlabel='day', ylabel='total_bill'>
![png](https://img-blog.csdnimg.cn/img_convert/ce02096b8cc21d395f945749d39eee09.png)
类别内数据分布
sns.boxplot(x="day",y="total_bill",data=data,hue="time",palette={"g","r"},saturation=0.5)
<AxesSubplot:xlabel='day', ylabel='total_bill'>
![png](https://img-blog.csdnimg.cn/img_convert/5ffbcf152a83978c1ae11e7d4e698cfb.png)
sns.violinplot(x="day",y="total_bill",data=data,hue="time")
<AxesSubplot:xlabel='day', ylabel='total_bill'>
![png](https://img-blog.csdnimg.cn/img_convert/e85b4d3826092bc52d691733fc3674e6.png)
类别内统计估计
sns.barplot(x="day",y="total_bill",data=data,hue="time")
<AxesSubplot:xlabel='day', ylabel='total_bill'>
![png](https://img-blog.csdnimg.cn/img_convert/d963e0bd2273143bf661fcd67f0944e8.png)
sns.pointplot(x="day",y="total_bill",data=data,hue="time")
<AxesSubplot:xlabel='day', ylabel='total_bill'>
![png](https://img-blog.csdnimg.cn/img_convert/1c3204990fe63ae4dadfae00b23cc397.png)