change axis in time series for a custom year

Question:

I have a database with a number of events for a given date. I want to display a graph showing the number of events each day, displaying the week number on the x-axis and creating a curve for each year.

I have no problem doing this simply.

My concern is that I should not display the "calendar" years (from January 1st to December 31st, in other words the isoweek from 1 to 53), but the so-called "winter" years regrouping the 12 month periods from August to August.

I wrote the code below to do this, I get a correctly indexed table with the number of weeks indexed in order (from 35 to 53 then from 53 to 34), the number of cases ("count") and a "winter_year" column which allows me to group my curves.

In spite of all my attempts, on the diagram the X axis continues to be displayed from 1 to 53 …

I have recreated an example with random numbers below
Can you help me to get the graph I want?

I am also open to any suggestions for improving my code which, I know, is probably not very optimal… I’m still learning Python.

#%%
import pandas
import numpy as np
from datetime import date

def winter_year(date):
    if date.month > 8:
        x = date.year
    else:
        x = date.year-1
    return("Winter "+str(x)+"-"+str((x+1)))  

#%%
np.random.seed(10)
data = pandas.DataFrame()
data["dates"] = pandas.date_range("2017-07-12","2022-08-10")
data["count"] = pandas.Series(np.random.randint(150, size = len(data["dates"])))
data = data.set_index("dates")
print(data)

#%%
data["week"] = data.index.isocalendar().week
data["year"] = data.index.year
data["date"] = data.index.date
data["winter_year"] = data["date"].apply(winter_year)
datapiv = pandas.pivot_table(data,values = ["count"],index = ["week"], columns = ["winter_year"],aggfunc=np.sum)


order_weeks = [i for i in range(35,54)]
for i in range(1,35):
    order_weeks.append(i)
datapiv = datapiv.reindex(order_weeks)

datapiv.plot(use_index=True)

img

Asked By: Aytan

||

Answers:

Add this line before the plot:

datapiv.index = [str(x) for x in order_weeks]

it would be something like:

...

order_weeks = [i for i in range(35,54)]
for i in range(1,35):
    order_weeks.append(i)
datapiv = datapiv.reindex(order_weeks)

# this is the new line
datapiv.index = [str(x) for x in order_weeks]

datapiv.plot(use_index=True)

Output:

enter image description here

Answered By: 99_m4n
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.