使用此代码:
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轴上具有频率密度。因此,我决定先尝试使用条形图,看看结果如何。条形图显示如下:
使用以下代码:
ax1 = counts1.plot(kind='bar',stacked = False)
,所以我想让我们看一下概率密度函数在将bar更改为“ kde”并得到如下所示的样子:我想这个图还可以,但是我对我的x轴并不真正满意。是否可以在x轴上索引每个z得分(可以说像我的条形图的x轴一样)?我是pandas / matplotlib /的新手,并且我正在尝试学习绘图,希望能对您有所帮助。
最佳答案
准备虚拟数据:
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()
绘图:
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()
情节后获得的儿童美术师
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>]