1

I'm trying to plot grouped by month DataFrame, where index column is DateTime.
My goal is to plot all months on separate plots.

index=date_range('2011-9-1 00:00:03', '2012-09-01  00:00:03', freq='10min')
df=pd.DataFrame(np.random.rand(len(index),3),index=index)
df2 = df.groupby(lambda x: x.month)
df2.plot()

This gives me 14 plots (not 12), where 2 first are empty - on the x-axis are years from 2000 to 2010. Than two first plots are January.

Hoping for your good advice how to cope with this.

4

1 回答 1

1

你想达到什么目的?在对数据进行分组时,如果要绘制数据,通常会以某种方式对其进行聚合。例如:

import pandas as pd
index=pd.date_range('2011-1-1 00:00:03', '2011-12-31 23:50:03', freq='10min')
df=pd.DataFrame(np.random.rand(len(index),3),index=index)
df2 = df.groupby(lambda x: x.month)
for key, group in df2:
    group.plot()

更新:修复了超过一个月的组。这可能不是最好的解决方案,但它是我首先想到的解决方案。

import pandas as pd

num_ticks = 15

index=pd.date_range('2011-9-1 00:00:03', '2012-09-01  00:00:03', freq='10min')
df=pd.DataFrame(np.random.rand(len(index),3),index=index)
df2 = df.groupby(lambda x: x.month)
for key, group in df2:
    step = len(group) / num_ticks
    reset = group.reset_index()
    reset.plot()
    plt.xticks(reset.index[::step],
               reset['index'][::step].apply(
                    lambda x: x.strftime('%Y-%m-%d')).values,
               rotation=70)
于 2013-08-26T20:02:58.437 回答