seaborn可视化

匿名 (未验证) 提交于 2019-12-03 00:19:01
# 查看当前挂载的数据集目录 !ls /home/kesci/input/ 
# 查看个人持久化工作区文件 !ls /home/kesci/work/ 
# 查看当前kernerl下的package !pip list --format=columns 
# 显示cell运行时长 %load_ext klab-autotime 
import matplotlib.pyplot as plt %matplotlib inline import seaborn as sns sns.set() import numpy as np import pandas as pd 
data=np.random.multivariate_normal([0,0],[[5,2],[2,2]],size=2000)  data=pd.DataFrame(data,columns=['x','y']) 
data.head() 
Out[9]:
xy
00.571117-0.158731
12.5227652.033863
2-3.413121-0.566827
3-1.788482-0.308131
43.5659472.668333
for col in 'xy':#频次直方图     plt.hist(data[col],normed=True,alpha=0.5) 
/opt/conda/lib/python3.5/site-packages/matplotlib/axes/_axes.py:6462: UserWarning: The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.   warnings.warn("The 'normed' kwarg is deprecated, and has been " 

for col in 'xy':#KDE可视化     sns.kdeplot(data[col],shade=True) 

sns.distplot(data['x'])#频次直方图与KDE的结合 sns.distplot(data['y']); 
/opt/conda/lib/python3.5/site-packages/matplotlib/axes/_axes.py:6462: UserWarning: The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.   warnings.warn("The 'normed' kwarg is deprecated, and has been " 

sns.kdeplot(data);#二维KDE图 
/opt/conda/lib/python3.5/site-packages/seaborn/distributions.py:645: UserWarning: Passing a 2D dataset for a bivariate plot is deprecated in favor of kdeplot(x, y), and it will cause an error in future versions. Please update your code.   warnings.warn(warn_msg, UserWarning) 

with sns.axes_style('white'):     sns.jointplot('x','y',data,kind='kde') 

with sns.axes_style('white'):     sns.jointplot('x','y',data,kind='hex') 
/opt/conda/lib/python3.5/site-packages/matplotlib/axes/_axes.py:6462: UserWarning: The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.   warnings.warn("The 'normed' kwarg is deprecated, and has been " 

iris=sns.load_dataset('iris') 
iris.head() 
Out[21]:
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
sns.pairplot(iris,hue='species',size=2.5)#矩阵图 
Out[24]:
<seaborn.axisgrid.PairGrid at 0x7f380c3ac0b8>

tips=sns.load_dataset('tips') tips.head() 
Out[26]:
total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4
tips['tip_pct']=100*tips['tip']/tips['total_bill']#分面频次直方图 grid=sns.FacetGrid(tips,row='sex',col='time',margin_titles=True) grid.map(plt.hist,'tip_pct',bins=np.linspace(0,40,15)); 

with sns.axes_style(style='ticks'):  # 因子图中不同离散因子分布对比     g = sns.factorplot('day', 'total_bill', 'sex', data=tips, kind='box')     g.set_axis_labels('Day', 'Total Bill') 

with sns.axes_style('white'):#联合分布图     sns.jointplot('total_bill','tip',data=tips,kind='hex') 
/opt/conda/lib/python3.5/site-packages/matplotlib/axes/_axes.py:6462: UserWarning: The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.   warnings.warn("The 'normed' kwarg is deprecated, and has been " 

sns.jointplot('total_bill','tip',data=tips,kind='reg')#带回归拟合的联合分布 
/opt/conda/lib/python3.5/site-packages/matplotlib/axes/_axes.py:6462: UserWarning: The 'normed' kwarg is deprecated, and has been replaced by the 'density' kwarg.   warnings.warn("The 'normed' kwarg is deprecated, and has been " 
Out[38]:
<seaborn.axisgrid.JointGrid at 0x7f37faf14dd8>

planets=sns.load_dataset('planets')#用行星数据 planets.head() 
Out[40]:
methodnumberorbital_periodmassdistanceyear
0Radial Velocity1269.3007.1077.402006
1Radial Velocity1874.7742.2156.952008
2Radial Velocity1763.0002.6019.842011
3Radial Velocity1326.03019.40110.622007
4Radial Velocity1516.22010.50119.472009
with sns.axes_style('white'):     g=sns.factorplot('year',data=planets,aspect=2,kind='count',color='steelblue')     g.set_xticklabels(step=5) 

with sns.axes_style('white'):#不同年份、方法发现的行星数量     g=sns.factorplot('year',data=planets,aspect=4.0,kind='count',hue='method',order=range(2001,2015))     g.set_ylabels('number of planets discovered') 

文章来源: seaborn可视化
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!