python - Pandas dataframe + groupby = failed zooming for x-axis ticks -


i trying plot pandas dataframe data but, when organised daily/monthly/yearly sums using groupby, resulting plot cannot zoomed correctly.

the zoom work x-axis tickmarks don't update correctly. can't work out solution this.

example code:

import datetime import pandas pd import numpy np  arraya = np.random.rand(1,100)[0] arrayb = np.random.rand(1,100)[0] arrayc = np.random.rand(1,100)[0] arrayd = np.random.rand(1,100)[0]  day_counts = {'a': arraya,               'b': arrayb,               'c': arrayc,               'd': arrayd}  #prepare data df_days = pd.dataframe(day_counts, index=pd.date_range('2012-01-01', periods=100)) #df_use = df_days.groupby([lambda x: x.year, lambda x: x.month, lambda x: x.day]).sum() df_use = df_days.groupby([lambda x: x.year, lambda x: x.month]).sum()  #prepare percentages df_use_perc = df_use.divide(df_use.sum(axis=1), axis=0).multiply(100) #percentages  my_colors = list(['orange', 'blue', 'purple', 'red'])  #plot main subfigure (relative event types) ax = df_use_perc.plot(kind='area', stacked=true, color=my_colors) 

it line causes failure:

df_use = df_days.groupby([lambda x: x.year, lambda x: x.month]).sum() 

i can plot using dataframe df_days, without using groupby function , works okay need able sum months etc.

plot: enter image description here

plot after zooming in massively (the whole x-axis few seconds wide): enter image description here

iiuc can following:

x = df_days.groupby(pd.timegrouper('ms')).sum() x.div(x.sum(1), 0).mul(100).plot(kind='area', stacked=true, color=my_colors) 

enter image description here

after zooming:

enter image description here

explanation:

in [35]: x out[35]:                              b          c          d 2012-01-01  14.739981  18.306502  11.659834  13.990243 2012-02-01  13.180681  12.487874  15.367421  16.877128 2012-03-01  14.528299  16.936493  16.467844  16.668185 2012-04-01   4.190121   3.110165   5.165066   3.086899  in [36]: x.div(x.sum(1), 0).mul(100) out[36]:                              b          c          d 2012-01-01  25.112171  31.188374  19.864594  23.834861 2012-02-01  22.759411  21.563123  26.535309  29.142158 2012-03-01  22.489341  26.217149  25.491695  25.801815 2012-04-01  26.942217  19.998167  33.211047  19.848569 

Comments