Eli Turasky
Eli Turasky

Reputation: 1061

Iteratively plot data through datetime in pandas dataframe

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

Upvotes: 0

Views: 105

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

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()
)

Upvotes: 1

Related Questions