Home > Back-end >  Plot area charts with different colors for time series data based on multiple condtions in Matplotli
Plot area charts with different colors for time series data based on multiple condtions in Matplotli

Time:11-12

Given a small dataset as follows:

         date  value  type
0  2021-07-31   6.32     1
1  2021-08-31   5.93     1
2  2021-09-30   5.43     2
3  2021-10-30   4.72     2
4  2021-11-30   4.23     3
5  2021-12-31   3.96     3

or:

df = pd.DataFrame({'date': {0: '2021-07-31',
  1: '2021-08-31',
  2: '2021-09-30',
  3: '2021-10-30',
  4: '2021-11-30',
  5: '2021-12-31'},
 'value': {0: 6.32, 1: 5.93, 2: 5.43, 3: 4.72, 4: 4.23, 5: 3.96},
 'type': {0: 1, 1: 1, 2: 2, 3: 2, 4: 3, 5: 3}})

I hope to draw area charts based on type, ie., for date where if type=1, 2, 3, I will use color gray, lightpink and skyblue respecitvely.

How could I do that? Thanks.

The expected plot will like this:

enter image description here

Reference code:

year_n_1 = [1.5, 3, 10, 13, 22, 36, 30, 33, 24.5, 15, 6.5, 1.2]
year_n = [2, 7, 14, 17, 20, 27, 30, 38, 25, 18, 6, 1]

plt.fill_between(np.arange(12), year_n_1, color="lightpink",
                 alpha=0.5, label='year N-1')
plt.fill_between(np.arange(12), year_n, color="skyblue",
                 alpha=0.5, label='year N')

plt.legend()
plt.show()

Out:

enter image description here

EDIT:

df = df.set_index('date')
colors = ['gray', 'lightpink', 'skyblue']
plt.fill_between(df['value'], color=colors[type], alpha=0.5)
plt.legend()
plt.show()

Out:

TypeError: list indices must be integers or slices, not type

CodePudding user response:

Try:

# let's use datetime type 
df['date'] = pd.to_datetime(df['date'])

# the colors
colors = ['gray', 'lightpink', 'skyblue']

# loop and plot
fig, ax = plt.subplots()
for i, (t, d) in enumerate(df.groupby('type')):
    d.plot.area(x='date', y='value', label=t, ax=ax, alpha=0.5, color=colors[i])
    
plt.show()

Output:

enter image description here

  • Related