3
 In[1]    df    
Out[0]    week  year    number_of_cases
            8   2010    583.0
            9   2010    116.0
           10   2010    358.0
           11   2010    420.0
           ...   ...      ...
           52   2010    300.0
            1   2011    123.0
            2   2011    145.0

How may I create a timeline graph where my y-axis is the number of cases and my x axis is increasing week number that corresponds with the year? I want it to go from week 1 to 52 in the year 2010 then week 1 to 52 in the year 2011. And have this as one large graph to see how the number of cases vary each year according to week.

Python 3, Pandas.

1 Answers1

0

You can create a datetime column based on the year and the week, plot 'number_of_cases' against the date, and then use mdates to format the x-ticks.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

# Determine the date
df['date'] = pd.to_datetime(df.assign(day=1, month=1)[['year', 'month', 'day']])+pd.to_timedelta(df.week*7, unit='days')

# Plot
fig, ax = plt.subplots()
df.plot(x='date', y='number_of_cases', marker='o', ax=ax)

# Format the x-ticks
myFmt = mdates.DateFormatter('%Y week %U')
ax.xaxis.set_major_formatter(myFmt)

enter image description here

ALollz
  • 57,915
  • 7
  • 66
  • 89