I'm trying to plot a colorbar below this chart, where the color depends on when each of the time series starts:

The code generated to create the plot is this:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
def partial_cum_returns(start, cum_returns):
return cum_returns.loc[start:].div(cum_returns.loc[start])
index = pd.DatetimeIndex(pd.date_range('20170101', '20190101', freq='W'))
np.random.seed(5)
returns = pd.Series(np.exp(np.random.normal(loc=0, scale=0.05, size=len(index))), index=index)
cum_returns = returns.cumprod()
df = pd.DataFrame(index=index)
for date in index:
df[date] = partial_cum_returns(date, cum_returns)
df.plot(legend=False, colormap='viridis');
plt.colorbar();
But when executing this error appears:
RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).
I've tried to add the colorbar in different ways, like the fig, ax = plt.figure()... one, but I couldn't make it work so far. Any ideas? Thanks!
