使用pandas / python上的Z分数绘制概率密度函数

时间:2016-12-13 14:26:57

标签: python pandas matplotlib plot

使用此代码:

df1 = (df.ix[:,1:] - df.ix[:,1:].mean()) / df.ix[:,1:].std()

我计算了一列上的z分数,其中第二列的分组数据框中的项目频率分布。现在结果看起来像这样:

Z Score     Frequency Distribution
-2.394214       1
-2.280489       1
-2.166763       2
-2.109900       7
-2.053037       4
-1.939311       7
-1.882448      11
-1.825586       9
-1.768723       7
-1.711860       4
-1.654997      11 ..about 73 items

现在我想创建一个概率密度图,其x轴为z分,y轴为频率密度。所以我决定先尝试条形图,看看结果如何。条形图显示如下:

enter image description here

使用此代码:ax1 = counts1.plot(kind='bar',stacked = False),所以我想让我们看看概率密度函数在我将其更改为“kde”的位置,并得到类似的结果:

enter image description here

我认为情节还可以,但我对x轴并不满意。是否有可能在x轴上索引每个z分数(比如我的条形图的x轴)?我是pandas / matplotlib /的新手,我正在努力学习绘图,感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

准备虚拟数据:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

np.random.seed([314, 42])
df = pd.DataFrame(dict(ZScore=np.sort(np.random.uniform(-2, 2, 50)), 
                       FreqDist=np.random.randint(1, 30, 50)))
df.head()

enter image description here

绘制:

ax = df.plot(x='ZScore', y='FreqDist', kind='kde', figsize=(10, 6))
# get the x axis values corresponding to this slice (See beneath the plot)
arr = ax.get_children()[0]._x
# take the first and last element of this array to constitute the xticks and 
# also rotate the ticklabels to avoid overlapping
plt.xticks(np.linspace(arr[0], arr[-1]), rotation=90)
plt.show()

enter image description here

在情节之后获得的list儿童艺术家的输出:

ax.get_children()
[<matplotlib.lines.Line2D at 0x1d68b5c6d68>, <--- first element in list of child artists
 <matplotlib.spines.Spine at 0x1d6895f14a8>,
 <matplotlib.spines.Spine at 0x1d6895f1f98>,
 <matplotlib.spines.Spine at 0x1d68d881828>,
 <matplotlib.spines.Spine at 0x1d68b995048>,
 <matplotlib.axis.XAxis at 0x1d689aeb978>,
 <matplotlib.axis.YAxis at 0x1d68d7ff908>,
 <matplotlib.text.Text at 0x1d689b55cf8>,
 <matplotlib.text.Text at 0x1d689b55a20>,
 <matplotlib.text.Text at 0x1d689b55c88>,
 <matplotlib.legend.Legend at 0x1d687645390>,
 <matplotlib.patches.Rectangle at 0x1d689b55080>]