1

I have a dataframe, stock_data.head():

       date     open    high    low     close    volume    Name
0   2013-02-08  15.07   15.12   14.63   14.75   8407500     AAL
1   2013-02-11  14.89   15.01   14.26   14.46   8882000     AAL
2   2013-02-12  14.45   14.51   14.10   14.27   8126000     AAL
3   2013-02-13  14.30   14.94   14.25   14.66   10259500    AAL
4   2013-02-14  14.94   14.96   13.16   13.99   31879900    AAL

Below I have a code for producing a groupby command from my dataframe:

avg_permonth = stock_data.set_index('date').groupby(pd.Grouper(freq='M'))
avg_permonth['volume'].mean()


date
2013-02-28    5.261789e+06
2013-03-31    4.825485e+06
2013-04-30    4.990292e+06
2013-05-31    4.836257e+06
2013-06-30    5.145598e+06
                  ...     
2017-10-31    3.903486e+06
2017-11-30    4.133801e+06
2017-12-31    3.919748e+06
2018-01-31    4.486669e+06
2018-02-28    6.249305e+06
Freq: M, Name: volume, Length: 61, dtype: float64

My question is, how can I split these results by year to then create subplots of date vs volume? (would I have to reset_index() first or can I plot the index as x axis?) I wish the for 5 subplots from 2013-02 to 2018-02.

I tried with this this SO post but did not give the outcome I was looking for - it was 5 subplots but the x axis was from 2013 - 2018 for each plot and all had the same plot.

I wish for the x axis on the first subplot to be from the first date value of 2013 to the last date value of 2013, the second plot for 2014 and so on.

Ben.T
  • 29,160
  • 6
  • 32
  • 54

1 Answers1

1

you can do it this way:

import matplotlib.pyplot as plt

# dummy values like your serie
s = pd.Series(range(12*6), 
              index=pd.date_range('2013-01-01', '2018-12-31', freq='M'), 
              name='volume')

# Create figure and axs for multiple Axes
fig, axs = plt.subplots(3, 2, figsize=(12,12))

# groupby year to plot each year un a subplot
for i, (year, sg) in enumerate(s.groupby(s.index.year)):
    # plot each year in a subplot
    sg.plot(ax=axs[i//2, i%2]) # here you can add several parameters for colors and stuff
    # or axs[i//2, i%2].plot(sg.index, sg.values) would give the same result
plt.show() 
Ben.T
  • 29,160
  • 6
  • 32
  • 54
  • This has an issue with the top line of the `for` loop: `AttributeError: Cannot access callable attribute 'groupby' of 'DataFrameGroupBy' objects, try using the 'apply' method `.In exchange for your `s.groupby(s.index.year)` I have put my version `avg_permonth.groupby(avg_permonth.index.year)`. Can I not do another `groupby` since it is already done? @Ben.T – pragmatic learner Apr 02 '20 at 14:38
  • @pragmaticlearner what I called `s` is actually the same structure that `avg_permonth['volume'].mean()` because I understood it is what you wanted to plot this, but if you goal is to plot all the points, then you can replace `s.groupby(s.index.year)` by `stock_data.set_index('date').groupby(pd.Grouper(freq='Y'))['volume']` e.g. – Ben.T Apr 02 '20 at 15:02