I have a dataframe containing a time series with two columns as follows:
dailyp kind
date
2015-01-01 165.0 national
2015-01-02 210.0 not_festive
2015-01-03 222.0 not_festive
2015-01-04 190.0 not_festive
2015-01-05 200.0 not_festive
... ... ...
2019-12-28 260.0 not_festive
2019-12-29 226.0 not_festive
2019-12-30 216.0 not_festive
2019-12-31 189.0 not_festive
2020-01-01 237.0 not_festive
I have written a function to plot the time series differing on the value of kind
that goes as follows:
def plot_timeseries_by_category(df, category_col):
# Get the unique years from the index
years = df.index.year.unique()
# Create a subplot for each year
fig, axes = plt.subplots(len(years), 1, figsize=(10, len(years) * 5))
if len(years) == 1:
axes = [axes]
for year, ax in zip(years, axes):
# Filter the data for the current year
df_year = df[df.index.year == year]
# Create all neccesary colors
colors = {category: f'C{index}' for index, category in enumerate(df[category_col].unique())}
print(df_year.index)
# Groupby category and plot
for category, group in df_year.groupby(category_col):
group.plot('index', 'dailyp', marker='o', ax=ax, color=colors[category], label=category)
ax.set_title(str(year))
ax.legend()
The code also breaks the time series by years, but that works just fine. But instead of plotting a single line with different colors depending on the category, it plots a line for each category. I want to achieve what is shown in the approved answer of this post Plot Multicolored line based on conditional in python, but couldn't make it work.
Any help is appreciated!