50 pandas直方图hist核密度图(tcy)

直方图核密度图

1.函数:

 df.plot.hist(by=None, bins=10, **kwds)< /FONT> #绘制df列直方图
说明:
    直方图是一种可以对值频率进行离散化显示的柱状图。
    数据点被拆分到离散的、间隔均匀的面元中,绘制的是各面元中数据点的数量
    最终得到数字频率分布直方图,X轴是df数值分布,Y轴是对应数值出现的次数参数:
参数:
    by:str或sequence;要分组DataFrame中的列
    bin:int,默认值为10;要使用的直方图bins箱数

返回:matplotlib.Axes.axes Subplot直方图。  

实例1:

data=np.random.randn(1000)
df=pd.DataFrame({'a':data+1, 'b':data, 'c':data-1})

df.plot.hist(stacked=True, bins=12, alpha=0.5)  

 

2.核密度函数:

 df.plot.density(bw_method=None, ind=None, **kwds)
scipy.stats.gaussian_kde#使用高斯核生成核密度估计图
说明:
       密度图,利用数值出现频率绘制的直方图进行曲线拟合,会得到密度图;
       绘制的图形是根据直方图得到的条状分布的顶点连接后得到的平滑曲线,
       X轴是DataFrame当中的数值分布,Y轴是密度(Density)。
参数:
      bw_method:str,scalar或callable,用于计算估计器带宽的方法。
                  str={'scott','silverman'}为默认值None用“scott”
      ind:NumPy数组或整数,估算PDF的评估点。
              如是默认None使用1000个等间隔点。
              如是NumPy数组,那么KDE在通过的点上进行评估
              如是一个整数使用“ind”个等间距点。
返回axes:matplotlib.axes.Axes或numpy.ndarray 

实例2:

 df.plot.density()  

 

 实例3:

 ax1=df.a.plot.hist(stacked=True, bins= 12,alpha=0.5,density=1)
df.a.plot.density(style='g--',ax=ax1)  

 

 实例4:

df['a'].plot.hist(orientation='horizontal', cumulative=True)
#该图是将DataFrame对象当中的a进行数值累加,并绘制横向直方图,横轴表示频率(Frequency),
# 纵轴表示数值,cumulative=True的效果是将Frequency的数值从大到小进行排列。 

df.diff().hist(color='k', alpha=0.5, bins=50) #等效df.diff().hist()
#该效果是将DataFrame当中column分开,即将a,b和c分开绘制成三张图  

 实例5:

ax1=df.a.plot.hist(stacked=True, bins=12, alpha=0.5,density=1)
ax11=df.a.plot.density(style='g--',ax=ax1)

ax2=df.b.plot.hist(stacked=True, bins=12, alpha=0.5,density=1,ax=ax11)
ax12=df.b.plot.density(style='r--',ax=ax2)

ax3=df.c.plot.hist(stacked=True, bins=12, alpha=0.5,density=1,ax=ax12)
ax13=df.c.plot.density(style='b--',ax=ax3)

猜你喜欢

转载自blog.csdn.net/tcy23456/article/details/86353793
50