I have this data:
sale = [10, 20, 30, 40, 43, 46, 49, 50, 60, 70, 80, 90, 100, 110, 120, 130]
season = ['Winter'] * 7 + ['Spring'] * 3 + ['Summer'] * 3 + ['Fall'] * 3
ind = pd.concat([pd.DataFrame(pd.date_range(start='2020-1-1', periods=7, freq='W')),
pd.DataFrame(pd.date_range(start='2020-4-1', periods=9, freq='MS'))]).values.reshape((16,))
df = pd.DataFrame({
'Sale': sale,
'Season': season },
index=ind,
)
that is:
Sale Season
2020-01-05 10 Winter
2020-01-12 20 Winter
2020-01-19 30 Winter
2020-01-26 40 Winter
2020-02-02 43 Winter
2020-02-09 46 Winter
2020-02-16 49 Winter
2020-04-01 50 Spring
2020-05-01 60 Spring
2020-06-01 70 Spring
2020-07-01 80 Summer
2020-08-01 90 Summer
2020-09-01 100 Summer
2020-10-01 110 Fall
2020-11-01 120 Fall
2020-12-01 130 Fall
and this color map:
colors_map = {'Winter': 'b',
'Spring': 'pink',
'Summer': 'y',
'Fall': 'orange'}
I can easily plot a line as below:
df.plot();
or plot a scatter plot as below:
plt.scatter(x=df.index, y=df['Sale'], c=df['Season'].map(colors_map))
However, I do not know how to plot a line but each segment of the having a different color based on the color map.
Here seems to be a similar question: Plotting multiple segments with colors based on some variable with matplotlib