Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot min max line plot in python pandas

Hi I have a data frame in the following format.

For simplicity i am showing the data categorized as years, but it has the quarterly data. I want to do a line plot with min max as shadow and mean as a line plot. I tried different ways to do it but i am not able to get it in the output i need shown below.

As an alternative a box plot with mean, min and max will also work.

Data format enter image description here

Output Needed

enter image description here

like image 273
Uma Maheshwaraa Avatar asked Dec 18 '22 08:12

Uma Maheshwaraa


1 Answers

IIUC, groupby YEAR and aggregate your Value column by max, min and mean, then plot mean and use fill_between to do the coloring inside max and min.

data = df.groupby('YEAR')['VALUE'].agg({'Low Value':'min','High Value':'max','Mean':'mean'})
data.reset_index(inplace=True)

ax  = data.plot(x='YEAR', y='Mean', c='white')
plt.fill_between(x='YEAR',y1='Low Value',y2='High Value', data=data)

enter image description here

like image 135
Scott Boston Avatar answered Dec 20 '22 20:12

Scott Boston