【数据分析可视化】seaborn介绍

2020-07-07 19:22:43 浏览数 (1)

存在意义

是matplotlib的扩展封装

简单使用

代码语言:javascript复制
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 
import seaborn as sns
%matplotlib inline
代码语言:javascript复制
/Users/bennyrhys/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
/Users/bennyrhys/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
/Users/bennyrhys/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
/Users/bennyrhys/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
代码语言:javascript复制
iris = pd.read_csv('/Users/bennyrhys/Desktop/数据分析可视化-数据集/homework/iris.csv')
代码语言:javascript复制
iris.head()

SepalLength

SepalWidth

PetalLength

PetalWidth

Name

0

5.1

3.5

1.4

0.2

Iris-setosa

1

4.9

3.0

1.4

0.2

Iris-setosa

2

4.7

3.2

1.3

0.2

Iris-setosa

3

4.6

3.1

1.5

0.2

Iris-setosa

4

5.0

3.6

1.4

0.2

Iris-setosa

需求:画一个花瓣(petal)和花萼(sepal)长度的散点图,并且点的颜色要区分鸢尾花的种类

代码语言:javascript复制
# 花的种类
iris.Name.unique()
代码语言:javascript复制
array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype=object)
代码语言:javascript复制
# 设置对应颜色字典
color_map = dict(zip(iris.Name.unique(),['blue','green','red']))
color_map
代码语言:javascript复制
{'Iris-setosa': 'blue', 'Iris-versicolor': 'green', 'Iris-virginica': 'red'}
代码语言:javascript复制
# 原本画图
for species, group in iris.groupby('Name'):
    plt.scatter(group['PetalLength'],group['SepalLength'],
               color = color_map[species],
               alpha=0.3,edgecolor=None,
               label = species)
plt.legend(frameon=True, title='None')
plt.xlabel('PetalLength')
plt.ylabel('SepalLength')
代码语言:javascript复制
Text(0, 0.5, 'SepalLength')
代码语言:javascript复制
# 封装之后的seaborn画图
sns.lmplot('PetalLength','SepalLength',iris, hue='Name', fit_reg=False)  
代码语言:javascript复制
<seaborn.axisgrid.FacetGrid at 0x1a2a36c150>

生态

0 人点赞