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)
得到
本文详细对比了Seaborn与matplotlib在数据可视化方面的差异,并重点介绍了Seaborn如何创建直方图、密度图、柱状图和热力图。同时,探讨了Seaborn设置图形显示效果的方法,包括plotting_context()和set_context()的使用,以及调色功能的高级应用。通过实例展示了Seaborn的强大可视化能力。
最低0.47元/天 解锁文章

1906

被折叠的 条评论
为什么被折叠?



