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.