0

I work with a lot of timeseries data and would love a way to simply plot it seasonally;

For example;

            A  B  C  D  E  F  G H I
01/01/2008  4  4  43 4  3 4  3  4 3
02/01/2008  43 3  4  3  34  3  4  3
03/01/2008 11 2  3 4  3  4  3 44 3 
.
.
.
07/08/2021 43 3  4  3  34  3  4  3
08/09/2021 43 3  4  3  34  3  4  3

Is there an efficient or python-y way to plot this so that it would resemble a seasonality chart but on daily granularity?

Something that may resemble the below?

enter image description here

Ideally this may also create a dataframe with yearly columns of data with the index being dd/mm date format to also use.

Any help much appreciated!

The Singularity
  • 2,428
  • 3
  • 19
  • 48
spcol
  • 437
  • 4
  • 15

3 Answers3

2
  • simple in plotly
  • have simulated your data with random data
  • key step is an x-axis that is constant across the years for seasonality. Have used dates in 2021 using day of year to generate a date in 2021. Second step is setting date format as year is irrelevant
  • clearly no seasonality in my data as it is random...
import numpy as np
import pandas as pd
import plotly.express as px

n = 365 * 14
df = pd.DataFrame(
    index=pd.date_range("1-jan-2008", periods=n),
    data={c: np.random.randint(1, 45, n) for c in list("ABCDEFGHI")},
)

fig = px.line(
    df.assign(
        year=df.index.year,
        doy=pd.to_datetime(df.index.day_of_year.values + (2021 * 1000), format="%Y%j"),
        value=df.mean(axis=1),
    ),
    x="doy",
    y="value",
    color="year",
    template="plotly_dark"
)

# just for demo purposes, make some traces invisible
for t in fig.data:
    if int(t["name"])<2016: t["visible"]="legendonly"

fig.update_layout(xaxis={"tickformat":"%d-%b"})


enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30
  • Can you update this answer? it can't be reproduced! why do we need to `doy ` in this visualization? – Mario Jan 02 '22 at 16:43
  • 1
    @Mario works for me pandas 1.3.4 plotly 5.5.0 running in a jupyter env. *doy* is used as it's consistent across the years for xaxis. – Rob Raymond Jan 02 '22 at 20:10
  • I just double-checked in google colab [notebook](https://colab.research.google.com/drive/1OehDvlw63sraQEWI6SfjHbkB_-9MseMz?usp=sharing) with default versions `pandas==1.1.5` and `plotly==4.4.1` and `Python 3.7.12` which still issue remains due to `AttributeError: 'DatetimeIndex' object has no attribute 'day_of_year'` but with those package versions you mentioned it works. – Mario Jan 03 '22 at 11:21
1

Please note that monitoring seasonality of time-series data is different from plotting time-series data. It is needed to decompose data into its components over time. you can check this answer. However, just to plot time-series data regardless of format of timestamps in dark background using plt.style.use('dark_background'), it could be as follow:

import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('dark_background')

colors = [
    '#08F7FE',  # teal/cyan
    '#FE53BB',  # pink
    '#F5D300',  # yellow
    '#00ff41'  # matrix green
             ]

df = pd.DataFrame({'A': [1, 3, 9, 5, 2, 1, 1],
                   'B': [4, 5, 5, 7, 9, 8, 6],
                   'C': [7, 5, 3, 1, 5, 9, 3],
                   'D': [3, 6, 7, 4, 3, 2, 1],
                  'date':['10-10-2016', '10-10-2017', '10-10-2018', '10-10-2019', '10-10-2020', '10-10-2021', '10-10-2022']})

# make sure the time column is actually time format
df['date']=pd.to_datetime(df['date'])

# set time as the index
df.set_index('date',inplace=True)

fig, ax = plt.subplots()

df.plot(marker='o', color=colors, ax=ax)

ax.figure.autofmt_xdate(rotation=45, ha='center')
plt.legend(loc='best')
plt.show()

img

If you want to make it much fancy you can follow Time series Visualization or Matplotlib Cyberpunk Style

in order to cover following issue:

Ideally this may also create a dataframe with yearly columns of data with the index being dd/mm date format to also use.

Based on this post, you can use import matplotlib.dates as md with desired date-format once you passed date index to x-axis:

df.plot(marker='o', color=colors, ax=ax)
ax.set_xticks(df.index)
ax.figure.autofmt_xdate(rotation=45, ha='center')

####### Use the below functions #######
import matplotlib.dates as md
dtFmt = md.DateFormatter('%d-%b') # define the formatting
ax.xaxis.set_major_formatter(dtFmt) # apply the format to the desired axis

plt.legend(loc='best')
plt.show()

img

Mario
  • 1,631
  • 2
  • 21
  • 51
-1

For plotting I suggest you to take a look at matplotlib. For dataframe you can use pandas

import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame(yourdata) #to create a dataframe
df.plot() #to plot your data or df.plot(x="A",y="Date") to select what to plot
df["NewDate"] = pd.to_datetime(df['Date'], format='%d/%m') #to create the the date column with format dd/mm (based on the date column you already have)
Cheen
  • 1
  • 1
    This still just plots the data as one line as opposed to a line per year to show the seasonality I'm afraid. – spcol Sep 08 '21 at 10:55
  • You are right, excuse my poor english I didn't understand the question right :) – Cheen Sep 08 '21 at 12:33