【數據分析可視化】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>

在這裏插入圖片描述

生態

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章