Seaborn簡單畫圖(一) -- 散點圖
阿新 • • 發佈:2019-02-02
資料分析彙總學習
import pandas as pd
import numpy as np
from pandas import Series, DataFrame
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# 開啟一個花瓣長寬資料檔案
f = open('iris.csv')
iris = pd.read_csv(f)
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
# 繪製的顏色字典,zip傳入倆個列表
iris.Name.unique()
array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype=object)
color_map = dict(zip(iris.Name.unique(), ['blue' ,'green','red']))
使用matplot
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='Name')
plt.xlabel('PetalLength' )
plt.ylabel('SepalLength')
使用Seaborn
sns.lmplot('PetalLength','SepalLength',iris,hue='Name', fit_reg=False)