0

I have a dataframe here that contains a value daily since 2000 (ignore the index).

        Extent  Date
6453    13.479  2001-01-01
6454    13.385  2001-01-02
6455    13.418  2001-01-03
6456    13.510  2001-01-04
6457    13.566  2001-01-05

I would like to make a plot where the x-axis is the day of the year, and the y-axis is the value. The plot would contain 20 different lines, with each line corresponding to the year of the data. Is there an intuitive way to do this using pandas, or is it easier to do with matplotlib?

Here is a quick paint sketch to illustrate.

enter image description here

Eli Turasky
  • 981
  • 2
  • 11
  • 28
  • 2
    Some years have 365 days, some have 366? – Quang Hoang Jun 18 '20 at 18:21
  • Yes, the leap years contain 366 days. – Eli Turasky Jun 18 '20 at 18:22
  • Similar/Related: [Plot pandas data frame with year over year data](https://stackoverflow.com/questions/30379789/plot-pandas-data-frame-with-year-over-year-data) - solved py pivoting the DataFrame. Also [Plotting pandas dataframe with years](https://stackoverflow.com/questions/30379789/plot-pandas-data-frame-with-year-over-year-data) – wwii Jun 18 '20 at 18:47

1 Answers1

1

One quick way is to plot x-axis as strings:

df['Date'] = pd.to_datetime(df['Date'])

(df.set_index([df.Date.dt.strftime('%m-%d'), 
               df.Date.dt.year])
   .Extent.unstack()
   .plot()
)
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • is there an easy way to amend this plot by adding a vertical dotted line for the current day of the plot? – Eli Turasky Jun 18 '20 at 18:48
  • @EliTurasky - https://stackoverflow.com/questions/19213789/how-do-you-plot-a-vertical-line-on-a-time-series-plot-in-pandas – wwii Jun 18 '20 at 18:52