pandas 如何在python pandas中绘制最小最大线图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43382051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:22:58  来源:igfitidea点击:

How to plot min max line plot in python pandas

pythonpandasmatplotlibplot

提问by Uma Maheshwaraa

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 formatenter image description here

数据格式在此处输入图片说明

Output Needed

需要的输出

enter image description here

在此处输入图片说明

回答by Scott Boston

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.

IIUC, groupby YEAR 并按最大值、最小值和平均值聚合您的值列,然后绘制平均值并使用 fill_between 在最大值和最小值内进行着色。

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

在此处输入图片说明