将 fill_between() 与 Pandas 数据系列一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18171079/
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-13 21:04:56  来源:igfitidea点击:

Using fill_between() with a Pandas Data Series

pythonmatplotlibpandas

提问by PythonLearner

I have graphed (using matplotlib) a time series and its associated upper and lower confidence interval bounds (which I calculated in Stata). I used Pandas to read the stata.csv output file and so the series are of type pandas.core.series.Series.

我已经绘制了(使用 matplotlib)时间序列及其相关的置信区间上限和下限(我在 Stata 中计算)。我使用 Pandas 读取 stata.csv 输出文件,因此该系列属于 pandas.core.series.Series 类型。

Matplotlib allows me to graph these three series on the same plot, but I wish to shade between the upper and lower confidence bounds to generate a visual confidence interval. Unfortunately I get an error, and the shading doesn't work. I think this is to do with the fact that the functions between which I wish to fill are pandas.core.series.Series.

Matplotlib 允许我在同一个图上绘制这三个系列,但我希望在上下置信界限之间添加阴影以生成视觉置信区间。不幸的是,我收到一个错误,并且阴影不起作用。我认为这与我希望填充的函数是 pandas.core.series.Series 的事实有关。

Another post on here suggests that passing my_series.value instead of my_series will fix this problem; however I cannot get this to work. I'd really appreciate an example.

此处的另一篇文章建议传递 my_series.value 而不是 my_series 将解决此问题;但是我无法让它发挥作用。我真的很感激一个例子。

回答by Phillip Cloud

As long as you don't have NaNvalues in your data, you should be okay:

只要NaN您的数据中没有值,就应该没问题:

In [78]: x = Series(linspace(0, 2 * pi, 10000))

In [79]: y = sin(x)

In [80]: fill_between(x.values, y.min(), y.values, alpha=0.5)

Which yields:

其中产生:

enter image description here

在此处输入图片说明