【数据分析可视化】seaborn实现直方图和密度图

2020-07-07 19:21:34 浏览数 (1)

代码语言:javascript复制
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
%matplotlib inline
代码语言:javascript复制
# 引入
import seaborn as sns
代码语言:javascript复制
/Users/bennyrhys/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
/Users/bennyrhys/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
/Users/bennyrhys/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)
代码语言:javascript复制
# 正态分布生成1000个数据
s1 = Series(np.random.randn(1000))

普通画

代码语言:javascript复制
# 直方图
plt.hist(s1)
代码语言:javascript复制
(array([  1.,  34., 105., 253., 330., 198.,  68.,  10.,   0.,   1.]),
 array([-3.48152703, -2.68232526, -1.88312349, -1.08392171, -0.28471994,
         0.51448183,  1.31368361,  2.11288538,  2.91208715,  3.71128892,
         4.5104907 ]),
 <a list of 10 Patch objects>)
代码语言:javascript复制
# 密度图 Series 的方法直接画.plot
s1.plot(kind='kde')
代码语言:javascript复制
<matplotlib.axes._subplots.AxesSubplot at 0x1a1e906a90>

seaborn画

代码语言:javascript复制
# 直方图,密度图.distplot()
# 参数 数据,分块,是否直方图,是否密度图,rug分布情况
sns.distplot(s1, bins=20, hist=True, kde=True, rug=True)
代码语言:javascript复制
<matplotlib.axes._subplots.AxesSubplot at 0x1a1faf2410>
代码语言:javascript复制
# 密度图
# 参数 数据,颜色填充, 颜色
sns.kdeplot(s1, shade=True, color='r')
代码语言:javascript复制
<matplotlib.axes._subplots.AxesSubplot at 0x1a2258d850>
代码语言:javascript复制
sns.rugplot(s1)
代码语言:javascript复制
<matplotlib.axes._subplots.AxesSubplot at 0x1a227783d0>

调用plot的方法(以前sns可以调用现在不行了)

代码语言:javascript复制
plt.plot(s1)
代码语言:javascript复制
[<matplotlib.lines.Line2D at 0x1a225d6650>]

0 人点赞