Pandas Scatter Plots具有X标签和美学上令人愉悦的格式

时间:2017-01-14 22:30:56

标签: python pandas matplotlib

我正在为我的数据集中的一堆不同属性创建散点图:

import numpy as np
for i, group in df.groupby('property_name'):
    plt.figure()
    group.plot(kind='scatter',x='Week', y='true_duration', title=str(i), grid=True)
    plt.xticks(np.arange(min(df['Week']),max(df['Week']), 2))
    plt.show()

这适用于为每个属性创建散点图;但是它们不是很好的散点图,如图所示:

scatterplot

我想修复它,因此它不会在x轴上重叠,我也希望它不会在两边都有那些奇怪的空白区域。

最后,他们是否可以在每个图表下方添加一个表格,并在该图表中添加一些描述性统计数据?真的只是寻找平均值,中位数,最大值和最小值。

感谢您的帮助!

我的熊猫df看起来像这样:

property_name    Week     true_duration
A                 1           3
A                 3           5
B                 1           2.4
C                 3           5
C                 4           6
C                 5           4
D                 2           1

1 个答案:

答案 0 :(得分:3)

<强> 设置
这是我对你所拥有的模拟

np.random.seed([3,1415])
df = pd.DataFrame(dict(Week=np.random.randint(33, 53, 20),
                       true_duration=np.random.randint(9, 18, 20)))
df.iloc[-1, :] = pd.Series(dict(Week=1, true_duration=7))

df.plot(kind='scatter',x='Week', y='true_duration', grid=True)
plt.xticks(np.arange(min(df['Week']),max(df['Week']), 2));

enter image description here

选项1
跳过plt.ticks

df.plot(kind='scatter',x='Week', y='true_duration', grid=True)

enter image description here

选项2
扩大蜱虫

df.plot(kind='scatter',x='Week', y='true_duration', grid=True)
plt.xticks(np.arange(min(df['Week']) - 5,max(df['Week']) + 5, 5));

enter image description here

选项3
旋转刻度

df.plot(kind='scatter',x='Week', y='true_duration', grid=True, rot=90)
plt.xticks(np.arange(min(df['Week']),max(df['Week']), 2));

enter image description here

选项4
以上任意组合

df.plot(kind='scatter',x='Week', y='true_duration', grid=True, rot=90)
plt.xticks(np.arange(min(df['Week']) - 5,max(df['Week']) + 5, 5));

enter image description here