Histograms直方图

直方图(Histogram)又称质量分布图。是一种统计报告图,由一系列高度不等的纵向条纹或线段表示数据分布的情况。 一般用横轴表示数据类型,纵轴表示分布情况。

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.

Observed data. If this is a Series object with a name attribute, the name will be used to label the data axis.

bins : argument for matplotlib hist(), or None, optional #设置矩形图数量

Specification of hist bins, or None to use Freedman-Diaconis rule.

hist : bool, optional #控制是否显示条形图

Whether to plot a (normed) histogram.

kde : bool, optional #控制是否显示核密度估计图

Whether to plot a gaussian kernel density estimate.

rug : bool, optional #控制是否显示观测的小细条(边际毛毯)

Whether to draw a rugplot on the support axis.

fit : random variable object, optional #控制拟合的参数分布图形

An object with fit method, returning a tuple that can be passed to a pdf method a positional arguments following an grid of values to evaluate the pdf on.

{hist, kde, rug, fit}_kws : dictionaries, optional

Keyword arguments for underlying plotting functions.

vertical : bool, optional #显示正交控制

If True, oberved values are on y-axis.

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Nov 23 19:45:44 2018
  4. @author: czh
  5. """
  6. %clear
  7. %reset -f
  8. # In[*]
  9. %matplotlib inline
  10. import numpy as np
  11. import pandas as pd
  12. from scipy import stats, integrate
  13. import matplotlib.pyplot as plt #导入
  14. import seaborn as sns
  15. sns.set(color_codes=True)#导入seaborn包设定颜色
  16. # In[*]
  17. np.random.seed(sum(map(ord, "distributions")))
  18. x = np.random.normal(size=500)
  19. sns.distplot(x, kde=False, rug=True);
  20. #kde=False关闭核密度分布,rug表示在x轴上每个观测上生成的小细条(边际毛毯)

第十一节:数据密度分布 - 图1
当绘制直方图时,你最需要确定的参数是矩形条的数目以及如何放置它们。利用bins可以方便设置矩形条的数量。如下所示:

  1. sns.distplot(x, bins=30, kde=False, rug=True);

第十一节:数据密度分布 - 图2

Kernel density estimaton核密度估计

核密度估计是在概率论中用来估计未知的密度函数,属于非参数检验方法之一。.由于核密度估计方法不利用有关数据分布的先验知识,对数据分布不附加任何假定,是一种从数据样本本身出发研究数据分布特征的方法,因而,在统计学理论和应用领域均受到高度的重视。

  1. sns.distplot(x, hist=False, rug=True);#关闭直方图,开启rug细条

第十一节:数据密度分布 - 图3

  1. sns.kdeplot(x, shade=True);#shade控制阴影

第十一节:数据密度分布 - 图4

绘制两个子图

  1. import seaborn as sns
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. mean, cov = [0, 2], [(1, .5), (.5, 1)]
  5. x, y = np.random.multivariate_normal(mean, cov, size=50).T
  6. plt.subplot(121)
  7. # 单变量kde
  8. sns.kdeplot(x, shade=True)
  9. plt.subplot(122)
  10. # 双变量kde
  11. sns.kdeplot(x, y, shade=True)

第十一节:数据密度分布 - 图5

Fitting parametric distributions拟合参数分布

可以利用distplot() 把数据拟合成参数分布的图形并且观察它们之间的差距,再运用fit来进行参数控制。

  1. x = np.random.gamma(6, size=200)#生成gamma分布的数据
  2. sns.distplot(x, kde=False, fit=stats.gamma);#fit拟合

第十一节:数据密度分布 - 图6

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Nov 23 19:47:08 2018
  4. @author: czh
  5. """
  6. %clear
  7. %reset -f
  8. # In[*]
  9. import numpy as np
  10. import seaborn as sns
  11. import matplotlib.pyplot as plt
  12. # In[*]
  13. sns.set(style="white", palette="muted", color_codes=True)
  14. rs = np.random.RandomState(10)
  15. # Set up the matplotlib figure
  16. f, axes = plt.subplots(2, 2, figsize=(7, 7), sharex=True)
  17. sns.despine(left=True)
  18. # Generate a random univariate dataset
  19. d = rs.normal(size=100)
  20. # Plot a simple histogram with binsize determined automatically
  21. sns.distplot(d, kde=False, color="b", ax=axes[0, 0])
  22. # Plot a kernel density estimate and rug plot
  23. sns.distplot(d, hist=False, rug=True, color="r", ax=axes[0, 1])
  24. # Plot a filled kernel density estimate
  25. sns.distplot(d, hist=False, color="g", kde_kws={"shade": True}, ax=axes[1, 0])
  26. # Plot a historgram and kernel density estimate
  27. sns.distplot(d, color="m", ax=axes[1, 1])
  28. plt.setp(axes, yticks=[])
  29. plt.tight_layout()

第十一节:数据密度分布 - 图7