Python绘图总结(seaborn篇)之数据分布

学习https://seaborn.pydata.org/index.html记录,描述不一定准确,具体请参考官网

%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import seaborn as sns
import matplotlib.pyplot as plt
# plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签
# plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号

# seaborn中文乱码解决方案
from matplotlib.font_manager import FontProperties
myfont=FontProperties(fname=r'C:\Windows\Fonts\simhei.ttf',size=14)
sns.set(font=myfont.get_name())

1、distplot() 直方图

np.random.seed(20180514)

x = np.random.normal(size=100)
sns.distplot(x)  # 默认

这里写图片描述

# 隐藏数据趋势kde,显示数据紧密度fit
sns.distplot(x, kde=False, fit=stats.gamma)

这里写图片描述

# 隐藏直方图 hist=False
sns.distplot(x, hist=False, rug=True) # rug=True表示显示 rugplot(x)

这里写图片描述

2、kdeplot() 线条

# 单变量 kdeplot()
sns.kdeplot(x, shade=True)
sns.kdeplot(x, bw=.2, label="bw: 0.2")
sns.kdeplot(x, bw=2, label="bw: 2")
plt.legend()

这里写图片描述

# 双变量 kdeplot()
f, ax = plt.subplots(figsize=(6, 6))
cmap = sns.cubehelix_palette(as_cmap=True, dark=0, light=1, reverse=True)
sns.kdeplot(df.x, df.y, cmap=cmap, n_levels=20, shade=True)

这里写图片描述

3、rugplot() 条码

sns.kdeplot(x, shade=True, cut=-2)  # cut默认为3
sns.rugplot(x)

这里写图片描述

f, ax = plt.subplots(figsize=(6, 6))
sns.kdeplot(df.x, df.y, ax=ax)            # kdeplot() 线条趋势
sns.rugplot(df.x, color="g", ax=ax)       # rugplot() X轴条码
sns.rugplot(df.y, vertical=True, ax=ax)   # rugplot() Y轴条码

这里写图片描述

4、jointplot() 散点图

mean, cov = [0, 1], [(1, .5), (.5, 1)]
data = np.random.multivariate_normal(mean, cov, 200)
df = pd.DataFrame(data, columns=["x", "y"])

sns.jointplot(x="x", y="y", data=df)

这里写图片描述

#  kind="kde" 线条趋势
sns.jointplot(x="x", y="y", data=df, kind="kde")

这里写图片描述

#  kind="kde" 六角形
x, y = np.random.multivariate_normal(mean, cov, 1000).T
with sns.axes_style("white"):
    sns.jointplot(x=x, y=y, kind="hex", color="k")

这里写图片描述

g = sns.jointplot(x="x", y="y", data=df, kind="kde", color="m")
g.plot_joint(plt.scatter, c="w", s=30, linewidth=1, marker="+")
g.ax_joint.collections[0].set_alpha(0)
g.set_axis_labels("X轴", "Y轴")
# plt.xlabel('X轴')

这里写图片描述

5、pairplot() 成对关系

# https://github.com/mwaskom/seaborn-data 数据地址
iris = sns.load_dataset("iris")
sns.pairplot(iris)

这里写图片描述

g = sns.pairplot(iris)
g.map_diag(sns.kdeplot)
g.map_offdiag(sns.kdeplot, cmap="Blues_d", n_levels=6)

这里写图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值