使用此代码:
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()
剧情后获得的 child 艺术家列表的输出:
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>]
