python - Add months to xaxis and legend on a matplotlib line plot -


i trying plot stacked yearly line graphs months. have dataframe df_year below:

day                    number of bicycle hires             2010-07-30                     6897  2010-07-31                     5564  2010-08-01                     4303  2010-08-02                     6642  2010-08-03                     7966 

with index set date going 2010 july 2017 july

i want plot line graph each year xaxis being months jan dec , total sum per month plotted

i have achieved converting dataframe pivot table below:

pt = pd.pivot_table(df_year, index=df_year.index.month, columns=df_year.index.year, aggfunc='sum') 

this creates pivot table below can plot show in attached figure:

  number of bicycle hires    2010      2011       2012      2013       2014    1                      nan  403178.0   494325.0  565589.0   493870.0    2                      nan  398292.0   481826.0  516588.0   522940.0    3                      nan  556155.0   818209.0  504611.0   757864.0    4                      nan  673639.0   649473.0  658230.0   805571.0    5                      nan  722072.0   926952.0  749934.0   890709.0   

plot showing yearly data months on xaxis

enter image description here

the problem months show integers , them shown jan, feb .... dec each line representing 1 year. , unable add legend each year.

i have tried following code achieve this:

dims = (15,5) fig, ax = plt.subplots(figsize=dims) ax.plot(pt)  months = monthlocator(range(1, 13), bymonthday=1, interval=1) monthsfmt = dateformatter("%b '%y") ax.xaxis.set_major_locator(months) #adding makes month ints disapper ax.xaxis.set_major_formatter(monthsfmt) handles, labels = ax.get_legend_handles_labels() #legend on plot ax.legend(handles, labels) 

please can me out this, doing incorrectly here?

thanks!


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -