Seaborn和matplotlib对比
#导入库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
#导入一个文件
iris = pd.read_csv('../homework/iris.csv')
iris.head()
得到
#需求:画一个花瓣(petal)和花萼(sepal)长度的散点图,并且点的颜色要区分鸢尾花的种类
iris.Name.unique()
'''
得到
array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype=object)
'''
#形成一个字典,根据种类区分颜色
color_map = dict(zip(iris.Name.unique(), ['blue','green','red']))
#for循环groupby方法
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)
得到
直方图和密度图
#导入库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
%matplotlib inline
#导入seaborn模块
import seaborn as sns
#生成Series
s1 = Series(np.random.randn(1000))
#直方图,使用Matplotlib plot
plt.hist(s1)
得到