当前位置:网站首页>Seaborn数据可视化
Seaborn数据可视化
2022-07-07 15:32:00 【En^_^Joy】
用Seaborn做数据可视化
Seaborn与Matplotlib
Matplotlib画图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.linspace(0, 10, 500)
y = np.cumsum(np.random.randn(500, 6), 0)
plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left')
# 显示图片
plt.show()
Seaborn
有许多高级的画图功能,而且可以改写Matplotlib
的默认参数,从而用简单的Matplotlib
获得更好的效果,可以用Seaborn
的set()
方法设置样式
其他代码和上面的一样,只是添加导入这个模块和使用set()
函数
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set()
plt.figure()
x = np.linspace(0, 10, 500)
y = np.cumsum(np.random.randn(500, 6), 0)
plt.plot(x, y)
plt.legend('ABCDEF', ncol=2, loc='upper left')
# 显示图片
plt.show()
Seaborn图形介绍
很多图形用Matplotlib都可以实现,三用Seaborn会更方便
频次直方图、KDE和密度图
频次直方图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
sns.set()
plt.figure()
data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000)
data = pd.DataFrame(data, columns=['x', 'y'])
for col in 'xy':
plt.hist(data[col], alpha=0.5)
# 显示图片
plt.show()
用KDE获取变量分布的平滑评估,通过sns.kdeplot
实现
for col in 'xy':
sns.kdeplot(data[col], shade=True)
用distplot
可以让频次直方图与KDE结合起来
for col in 'xy':
sns.distplot(data[col])
如果想kdeplot输入的是二维数据集,那么可以获得一个二维数可视化
sns.kdeplot(data['x'],data['y'])
用sns.jointplot可以同时看到两个变量的联合分布与单变量的独立分布,在这里使用白色背景
with sns.axes_style('white'):
sns.jointplot("x", "y", data, kind='kde')
with sns.axes_style('white'):
sns.jointplot("x", "y", data, kind='hex')
矩阵图
下面是四个变量之间关系的矩阵图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
sns.set()
plt.figure()
iris = sns.load_dataset("iris")
print(iris.head())
''' sepal_length sepal_width petal_length petal_width species 0 5.1 3.5 1.4 0.2 setosa 1 4.9 3.0 1.4 0.2 setosa 2 4.7 3.2 1.3 0.2 setosa 3 4.6 3.1 1.5 0.2 setosa 4 5.0 3.6 1.4 0.2 setosa '''
sns.pairplot(iris, hue='species', size=2.5)
# 显示图片
plt.show()
分面频次直方图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
sns.set()
plt.figure()
tips = sns.load_dataset("tips")
print(tips)
''' total_bill tip sex smoker day time size 0 16.99 1.01 Female No Sun Dinner 2 1 10.34 1.66 Male No Sun Dinner 3 2 21.01 3.50 Male No Sun Dinner 3 3 23.68 3.31 Male No Sun Dinner 2 4 24.59 3.61 Female No Sun Dinner 4 .. ... ... ... ... ... ... ... 239 29.03 5.92 Male No Sat Dinner 3 240 27.18 2.00 Female Yes Sat Dinner 2 241 22.67 2.00 Male Yes Sat Dinner 2 242 17.82 1.75 Male No Sat Dinner 2 243 18.78 3.00 Female No Thur Dinner 2 [244 rows x 7 columns] '''
tips['tip_pct'] = 100*tips['tip']/tips['total_bill']
grid = sns.FacetGrid(tips, row="sex", col="time", margin_titles=True)
grid.map(plt.hist, "tip_pct", bins=np.linspace(1, 40, 15))
# 显示图片
plt.show()
因子图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
sns.set()
plt.figure()
tips = sns.load_dataset("tips")
tips['tip_pct'] = 100*tips['tip']/tips['total_bill']
with sns.axes_style(style='ticks'):
g = sns.factorplot("day", "total_bill", "sex", data=tips, kind="box")
g.set_axis_labels("day", "Total Bill")
# 显示图片
plt.show()
联合分布
with sns.axes_style('white'):
sns.jointplot("total_bill", "tip", data=tips, kind='hex')
with sns.axes_style('white'):
sns.jointplot("total_bill", "tip", data=tips, kind='reg')
条形图
:sns.factorplot
画条形图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
sns.set()
plt.figure()
planets = sns.load_dataset('planets')
print(planets.head())
''' method number orbital_period mass distance year 0 Radial Velocity 1 269.300 7.10 77.40 2006 1 Radial Velocity 1 874.774 2.21 56.95 2008 2 Radial Velocity 1 763.000 2.60 19.84 2011 3 Radial Velocity 1 326.030 19.40 110.62 2007 4 Radial Velocity 1 516.220 10.50 119.47 2009 '''
with sns.axes_style('white'):
g = sns.factorplot("year", data=planets, aspect=2, kind="count", color='steelblue')
g.set_xticklabels(step=5)
# 显示图片
plt.show()
with sns.axes_style('white'):
g = sns.factorplot("year", data=planets, aspect=4.0, kind="count", hue='method', order=range(2001, 2015))
g.set_ylabels('Number of Planets Discovered')
边栏推荐
- DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
- 字节跳动Android面试,知识点总结+面试题解析
- 谎牛计数(春季每日一题 53)
- 深度监听 数组深度监听 watch
- QT 图片背景色像素处理法
- time标准库
- Spark Tuning (III): persistence reduces secondary queries
- Find tags in prefab in unity editing mode
- skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
- 预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
猜你喜欢
QML初学
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
Introduction and use of gateway
node:504报错
AutoLISP series (2): function function 2
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
Lowcode: four ways to help transportation companies enhance supply chain management
null == undefined
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
time标准库
随机推荐
SqlServer2014+: 创建表的同时创建索引
A tour of gRPC:03 - proto序列化/反序列化
LocalStorage和SessionStorage
LeetCode 1654. 到家的最少跳跃次数 每日一题
LeetCode 1981. 最小化目标值与所选元素的差 每日一题
Pycharm IDE下载
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
[Android -- data storage] use SQLite to store data
LeetCode 213. 打家劫舍 II 每日一题
【DesignMode】模板方法模式(Template method pattern)
蓝桥杯 决赛 异或变换 100分
3000 words speak through HTTP cache
Interface oriented programming
掌握这个提升路径,面试资料分享
LeetCode 1186. 删除一次得到子数组最大和 每日一题
1亿单身男女“在线相亲”,撑起130亿IPO
Deep listening array deep listening watch
ORACLE进阶(六)ORACLE expdp/impdp详解
运算符
AutoLISP series (3): function function 3