Python,最有效的方式来绘制pandas数据帧

时间:2018-03-27 13:49:47

标签: python-3.x pandas matplotlib

我正在尝试绘制9的子图(在此示例中,但该数字对于其他用例而言是可变的)线图,显示按县/区域划分的数据点数。

到目前为止,我有:

Surrey1 = df[df.county == 'Surrey']
Surrey2 = Surrey1.county.groupby(df.date_stamp).value_counts()


East_Sussex1 = df[df.county == 'East Sussex']
East_Sussex2 = East_Sussex1.county.groupby(df.date_stamp).value_counts()

West_Sussex1 = df[df.county == 'West Sussex']
West_Sussex2 = West_Sussex1.county.groupby(df.date_stamp).value_counts()


Buck1 = df[df.county == 'Buckinghamshire']
Buck2 = Buck1.county.groupby(df.date_stamp).value_counts()


Norfolk1 = df[df.county == 'Norfolk']
Norfolk2 = Norfolk1.county.groupby(df.date_stamp).value_counts()


Suffolk1 = df[df.county == 'Suffolk']
Suffolk2 = Suffolk1.county.groupby(df.date_stamp).value_counts()

Essex1 = df[df.county == 'Essex']
Essex2 = Essex1.county.groupby(df.date_stamp).value_counts()

Kent1 = df[df.county == 'Kent']
Kent2 = Kent1.county.groupby(df.date_stamp).value_counts()

# Create the fig
fig, axes = plt.subplots(nrows=8, ncols=1, figsize=(12,6))

# Now plot
pd1_N.plot(ax = axes[0], subplots=True, legend=False) 
pd2_S.plot(ax = axes[1], subplots=True, legend=False)
pd3_ES.plot(ax = axes[2], subplots=True, legend=False)
pd4_WS.plot(ax = axes[3], subplots=True, legend=False)
pd5_B.plot(ax = axes[4], subplots=True, legend=False)
pd6_S.plot(ax = axes[5], subplots=True, legend=False)
pd7_E.plot(ax = axes[6], subplots=True, legend=False)
pd8_K.plot(ax = axes[7], subplots=True, legend=False)

产生:

enter image description here

有更快/更有效的方法吗?关于如何使图表更具可呈现性的提示也将受到赞赏! 更新:

我现在正在使用一个非常简单的函数来更快地为变量指标执行此操作:

def plot_freq(metric, graph_width, graph_height):
    plot = str(metric)
    df.groupby(plot)['date_stamp'].value_counts().unstack(0).plot(subplots=True, figsize=(graph_width, graph_height))
    print("This plot shows the number of data points by", metric)

enter image description here

1 个答案:

答案 0 :(得分:4)

我认为你可以将其浓缩为以下内容:

df = pd.DataFrame({'Date':np.random.choice(pd.date_range('2017-10-01','2017-10-10',freq='D'), 500),'county':np.random.choice(['East Sussex','Buckinghamshire','Kent','Essex','Essex'],500)})

df.groupby('county')['Date'].value_counts().unstack(0).plot(subplots=True)

输出:

enter image description here