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)