user4740374
user4740374

Reputation: 109

Labeling year on time series

I am working on a timeseries plot from data that looks like the following:

import pandas as pd
  
data = {'index': [1, 34, 78, 900, 1200, 5000, 9001, 12000, 15234, 23432],
        'rating': [90, 85, 89, 82, 78, 65, 54, 32, 39, 45],
        'Year': [2005, 2005, 2005, 2006, 2006, 2006, 2007, 2008, 2009, 2009]}

df = pd.DataFrame(data)

The main issue is the lack of actual dates. I have plotted the data using the index order - the data is sorted in index-ascending order, the value of the index is meaningless.

I have plotted the data using

import plotly.express as px

fig = px.line(df, x='index', y='rating')
fig.show()

but would like to shade or label each year on the plot (could just be vertical dotted lines separating years, or alternated grey shades beneath the line but above the axis per year).

Upvotes: 1

Views: 107

Answers (1)

Prateek Verma
Prateek Verma

Reputation: 85

I am assuming that you have already sorted the DataFrame using the index column.

Here's a solution using bar (column) chart using matplotlib.

import matplotlib.pyplot as plt
import numpy as np

# [optional] create a dictionary of colors with year as keys. It is better if this is dynamically generated if you have a lot of years.
color_cycle = {'2005': 'red', '2006': 'blue', '2007': 'green', '2008': 'orange', '2009': 'purple'}

# I am assuming that the rating data is sorted by index already

# plot rating as a column chart using equal spacing on the x-axis
plt.bar(x=np.arange(len(df)), height=df['rating'], width=0.8, color=[color_cycle[str(year)] for year in df['Year']])

# add Year as x-axis labels
plt.xticks(np.arange(len(df)), df['Year'])
# add labels to the axes
plt.xlabel('Year')
plt.ylabel('Rating')

# display the plot
plt.show()

Outputs Column plot of rating vs Year

Upvotes: 1

Related Questions