(1)distplot,seaborn的displot()函数集合了matplotlib的hist()与核函数估计kdeplot的功能,增加了rugplot分布观测条显示与利用scipy库fit拟合参数分布的新颖用途。
(2)kdeplot,核密度估计的步骤:每一个观测附近用一个正态分布曲线近似;叠加所有观测的正态分布曲线;归一化
bandwidth(bw参数)用于近似的正态分布曲线的宽度。
(3)模型参数拟合,distplot的fit参数
(1.1)jointplot,kind参数默认——散点图
(1.2)jointplot,kind=’hex’——六角箱图
(1.3)jointplot,kind=’kde’——核密度估计图
(2)subplots上画kdeplot,rugplot
(3)cubehelix_palette,梦幻效果
(4)plot_joint和jointplot联合使用,同时画散点和二维kde
(1)pairplot,默认对角线histgram,非对角线kdeplot
(2)map_diag定义对角线单个属性图,map_offdiag定义非对角线两个属性关系图
——————————————————————————————
(1)distplot,seaborn的displot()函数集合了matplotlib的hist()与核函数估计kdeplot的功能,增加了rugplot分布观测条显示与利用scipy库fit拟合参数分布的新颖用途。具体用法如下:
seaborn.distplot(a, bins=None, hist=True, kde=True, rug=False, fit=None, hist_kws=None, kde_kws=None, rug_kws=None, fit_kws=None, color=None, vertical=False, norm_hist=False, axlabel=None, label=None, ax=None)
Parameters:
a : Series, 1d-array, or list. #一维数组
bins : argument for matplotlib hist(), or None, optional #设置分桶数
hist : bool, optional #控制是否显示分桶柱子
kde : bool, optional #控制是否显示核密度估计图
rug : bool, optional #控制是否显示观测实例竖线
fit : random variable object, optional #控制拟合的参数分布图形
{hist, kde, rug, fit}_kws : dictionaries, optional
Keyword arguments for underlying plotting functions.
vertical : bool, optional #显示正交控制
If True, oberved values are on y-axis.
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
#ord()函数它以一个字符(长度为1的字符串)作为参数,返回对应的ASCII数值,或者Unicode数值,
#如果所给的Unicode字符超出了你的Python定义范围,则会引发一个TypeError的异常。
#利用np.random.seed()函数设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数
x = np.random.normal(size=100)
sns.distplot(x, kde=True, bins=20, rug=True)
kde控制是否画kde曲线,bins是分桶数,rug控制是否画样本点(参考rug图可以设定合理的bins)
(2)kdeplot,核密度估计的步骤:每一个观测附近用一个正态分布曲线近似;叠加所有观测的正态分布曲线;归一化
bandwidth(bw参数)用于近似的正态分布曲线的宽度。
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
x = np.random.normal(size=100)
sns.kdeplot(x)
sns.kdeplot(x, bw=.2, label="bw: 0.2")
sns.kdeplot(x, bw=2, label="bw: 2")
plt.legend()
(3)模型参数拟合,distplot的fit参数
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
x = np.random.gamma(6, size=200)
sns.distplot(x, kde=False, fit=stats.gamma)
#fit控制拟合的参数分布图形
(1.1)jointplot,kind参数默认——散点图
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
mean, cov = [0, 1], [(1, .5), (.5, 1)]
#两个相关的正态分布
data = np.random.multivariate_normal(mean, cov, 200)
#依据指定的均值和协方差生成数据,生成二维数组
df = pd.DataFrame(data, columns=["x", "y"])
with sns.axes_style("ticks"):
sns.jointplot(x="x", y="y", data=df)
(1.2)jointplot,kind=’hex’——六角箱图
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
x, y = np.random.multivariate_normal(mean, cov, 200).T
with sns.axes_style("ticks"):
sns.jointplot(x=x, y=y, kind="hex")
(1.3)jointplot,kind=’kde’——核密度估计图
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
x, y = np.random.multivariate_normal(mean, cov, 200).T
with sns.axes_style("ticks"):
sns.jointplot(x=x, y=y, kind="kde")
#mean, cov = [0, 1], [(1, .5), (.5, 1)]
#data = np.random.multivariate_normal(mean, cov, 200)
#df = pd.DataFrame(data, columns=["x", "y"])
#with sns.axes_style("ticks"):
# sns.jointplot(x="x", y="y", data=df, kind="kde")
(2)subplots上画kdeplot,rugplot
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
x, y = np.random.multivariate_normal(mean, cov, 200).T
f, ax = plt.subplots(figsize=(6, 6))
sns.kdeplot(df.x, df.y, ax=ax)
sns.rugplot(df.x, color="g", ax=ax)
sns.rugplot(df.y, vertical=True, ax=ax)
(3)cubehelix_palette,梦幻效果
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
x, y = np.random.multivariate_normal(mean, cov, 200).T
f, ax = plt.subplots(figsize=(6, 6))
cmap = sns.cubehelix_palette(as_cmap=True, dark=1, light=0)
sns.kdeplot(df.x, df.y, cmap=cmap, n_levels=60, shade=True)
(4)plot_joint和jointplot联合使用,同时画散点和二维kde
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
np.random.seed(sum(map(ord, "distributions")))
mean, cov = [0, 1], [(1, .5), (.5, 1)]
data = np.random.multivariate_normal(mean, cov, 200)
df = pd.DataFrame(data, columns=["x", "y"])
g = sns.jointplot(x="x", y="y", data=df, kind="kde", color="y")
g.plot_joint(plt.scatter, c="m", s=30, linewidth=1, marker="+")
g.ax_joint.collections[0].set_alpha(0) #画背景网格线
g.set_axis_labels("$X$", "$Y$")
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
iris = sns.load_dataset("iris")
iris.head()
(1)pairplot,默认对角线histgram,非对角线kdeplot
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
iris = sns.load_dataset("iris")
sns.pairplot(iris);
(2)map_diag定义对角线单个属性图,map_offdiag定义非对角线两个属性关系图
%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes=True)
iris = sns.load_dataset("iris")
g = sns.PairGrid(iris)
g.map_diag(sns.kdeplot)
g.map_offdiag(sns.kdeplot, cmap="Blues_d", n_levels=20)