1

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
ah bon
  • 9,293
  • 12
  • 65
  • 148
  • Not totally clear what you are trying to do, do you want bar charts for each day with different colors, or areas like in your example? How does the data tie into your example? In terms of trying a solution, can you not just have a `list` or `dictionary` of colors that you reference? Something like `colors = ['gray', 'lightpink', 'skyblue']` and then in your plotting command, `color=colors[type]`. – ramzeek Nov 12 '21 at 06:05
  • Slightly different from the example plot since my data is time series, so I hope to plot it on three areas charts with different colors, the x-axis is `date`. Please see the edit code. – ah bon Nov 12 '21 at 06:13
  • I add an expected plot in the question, please check. – ah bon Nov 12 '21 at 06:16
  • 1
    Hi, the fill_between function does not work like that, please read https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.fill_between.html. Your error is due to the fact that color is a list and type is a string and can not slice or index a list with a string. You should put it insde a for loop and call something like colors[i] where i: 0,1,2. – MBV Nov 12 '21 at 06:20

1 Answers1

1

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

ah bon
  • 9,293
  • 12
  • 65
  • 148
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • Thanks a lot, off the topic, do you mind answer my question in the last post: https://stackoverflow.com/questions/69937972/groupby-year-month-and-drop-columns-with-all-nans-in-python – ah bon Nov 12 '21 at 06:26