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

存在意义

是matplotlib的扩展封装

简单使用

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 
import seaborn as sns
%matplotlib inline
/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)
iris = pd.read_csv('/Users/bennyrhys/Desktop/数据分析可视化-数据集/homework/iris.csv')
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)长度的散点图,并且点的颜色要区分鸢尾花的种类

# 花的种类
iris.Name.unique()
array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype=object)
# 设置对应颜色字典
color_map = dict(zip(iris.Name.unique(),['blue','green','red']))
color_map
{'Iris-setosa': 'blue', 'Iris-versicolor': 'green', 'Iris-virginica': 'red'}
# 原本画图
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')

在这里插入图片描述

Text(0, 0.5, 'SepalLength')
# 封装之后的seaborn画图
sns.lmplot('PetalLength','SepalLength',iris, hue='Name', fit_reg=False)  
<seaborn.axisgrid.FacetGrid at 0x1a2a36c150>

在这里插入图片描述

生态

在这里插入图片描述
在这里插入图片描述

原创文章 287 获赞 398 访问量 19万+

猜你喜欢

转载自blog.csdn.net/weixin_43469680/article/details/105987878
今日推荐