数据分析05绘图和可视化之Seaborn

本文详细对比了Seaborn与matplotlib在数据可视化方面的差异,并重点介绍了Seaborn如何创建直方图、密度图、柱状图和热力图。同时,探讨了Seaborn设置图形显示效果的方法,包括plotting_context()和set_context()的使用,以及调色功能的高级应用。通过实例展示了Seaborn的强大可视化能力。
摘要由CSDN通过智能技术生成

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)

得到

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值