[toc]
1、Seaborn介绍
Seaborn是基于matplotlib的图形可视化python包。它提供了一种高度交互式界面,便于用户能够做出各种有吸引力的统计图表。
Seaborn是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn能做出很具有吸引力的图,而使用matplotlib就能制作具有更多特色的图。应该把Seaborn视为matplotlib的补充,而不是替代物。
2、安装
pip install seaborn -i https://pypi.tuna.tsinghua.edu.cn/simple
教程
3、快速上手
3.1、样式设置
1 2
| import seaborn as sns sns.set(style = 'darkgrid',context = 'talk',font = 'STKaiti')
|
stlyle设置,修改主题风格,属性如下:
| style |
效果 |
| darkgrid |
黑色网格(默认) |
| whitegrid |
白色网格 |
| dark |
黑色背景 |
| white |
白色背景 |
| ticks |
四周有刻度线的白背景 |
context设置,修改大小,属性如下:
| context |
效果 |
| paper |
越来越大越来越粗 |
| notebook(默认) |
越来越大越来越粗 |
| talk |
越来越大越来越粗 |
| poster |
越来越大越来越粗 |
3.2、线形图
1 2 3 4 5 6 7 8 9 10 11 12
| import seaborn as sns import matplotlib.pyplot as plt import pandas as pd import numpy as np sns.set(style = 'dark',context = 'poster',font = 'STKaiti') plt.figure(figsize=(9,6))
x = np.linspace(0,2*np.pi,20) y = np.sin(x)
sns.lineplot(x = x,y = y,color = 'green',ls = '--') sns.lineplot(x = x,y = np.cos(x),color = 'red',ls = '-.')
|

4、各种图形绘制
4.1、调色板
参数palette(调色板),用于调整颜色,系统默认提供了六种选择:deep, muted, bright, pastel, dark, colorblind
参数palette调色板,可以有更多的颜色选择,Matplotlib为我们提供了多大178种,这足够绘图用,可以通过代码**print(plt.colormaps())**查看选择
| 178种 |
| Accent |
| Accent_r |
| Blues |
| Blues_r |
| …… |
4.2、线形图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import seaborn as sns import matplotlib.pyplot as plt import pandas as pd sns.set(style = 'dark',context = 'notebook',font = 'STKaiti') plt.figure(figsize=(9,6)) fmri = pd.read_csv('./fmri.csv')
ax = sns.lineplot(x = 'timepoint',y = 'signal', hue = 'event',style = 'event' , data= fmri, palette='deep', markers=True, markersize = 10)
plt.xlabel('时间节点',fontsize = 30) plt.savefig('./线形图.png',dpi = 200)
|
lineplot()函数作用是绘制线型图。参数x、y,表示横纵坐标;参数hue,表示根据属性分类绘制两条线(“event"属性分两类"stim”、“cue”);参数style,表示根据属性分类设置样式,实线和虚线;参数data,表示数据;参数marker、markersize,分别表示画图标记点以及尺寸大小!

4.3、散点图
1 2 3 4 5 6 7 8 9
| import matplotlib.pyplot as plt import seaborn as sns data = pd.read_csv('./tips.csv') plt.figure(figsize=(9,6)) sns.set(style = 'darkgrid',context = 'talk')
fig = sns.scatterplot(x = 'total_bill', y = 'tip', hue = 'time', data = data, palette = 'autumn', s = 100)
|

4.4、柱状图
1 2 3 4 5 6 7 8 9
| import seaborn as sns import matplotlib.pyplot as plt plt.figure(figsize = (9,6)) sns.set(style = 'whitegrid') tips = pd.read_csv('./tips.csv') ax = sns.barplot(x = "day", y = "total_bill", data = tips,hue = 'sex', palette = 'colorblind', capsize = 0.2)
|

4.5、箱式图
1 2 3 4 5 6
| import seaborn as sns import matplotlib.pyplot as plt import pandas as pd sns.set(style = 'ticks') tips = pd.read_csv('./tips.csv') ax = sns.boxplot(x="day", y="total_bill", data=tips,palette='colorblind')
|

4.6、直方图
1 2 3 4 5 6
| import seaborn as sns import numpy as np import matplotlib.pyplot as plt sns.set(style = 'dark') x = np.random.randn(5000) sns.histplot(x,kde = True)
|

1 2 3 4 5 6 7
| import seaborn as sns import numpy as np import matplotlib.pyplot as plt import pandas as pd sns.set(style = 'darkgrid') tips = pd.read_csv('./tips.csv') sns.histplot(x = 'total_bill', data = tips, kde = True)
|

4.7、分类散点图
1 2 3 4 5 6
| import seaborn as sns import matplotlib.pyplot as plt import pandas as pd sns.set(style = 'darkgrid') exercise = pd.read_csv('./exercise.csv') sns.catplot(x="time", y="pulse", hue="kind", data=exercise)
|

4.8、热力图
1 2 3 4 5 6 7 8
| import matplotlib.pyplot as plt import seaborn as sns plt.figure(figsize=(12,9)) flights = pd.read_csv('./flights.csv')
flights = flights.pivot("month", "year", "passengers") sns.heatmap(flights, annot=True,fmt = 'd',cmap = 'RdBu_r', linewidths=0.5)
|
