使用matplotlib填充曲线下方的区域

问题描述 投票:2回答:2

我有熊猫系列,

series

这是图形:

function

我想填充曲线下的区域。问题是调用

plt.fill(y)

输出:

enter image description here

正如在其他answers中看到的,这是因为我们需要向该函数发送一个多边形,因此我们必须添加一个(0,0)点。 (还有(lastPoint,0),但是在这种情况下是没有必要的。)

但是,建议的解决方案正在编写以下代码:

plt.fill([0]+[*range(0,len(y))],  [0]+pd.Series.tolist(y))

我拒绝相信这是最好的解决方案。

该代码太可怕了,一点都不容易阅读,我正在丢失信息(x轴上没有日期:] >>

enter image description here

此外,如果我同时调用绘图和填充(将红线放在顶部,则会发生错误:

/usr/local/anaconda3/lib/python3.7/site-packages/matplotlib/dates.py in refresh(self)
   1446     def refresh(self):
   1447         'Refresh internal information based on current limits.'
-> 1448         dmin, dmax = self.viewlim_to_dt()
   1449         self._locator = self.get_locator(dmin, dmax)
   1450 

/usr/local/anaconda3/lib/python3.7/site-packages/matplotlib/dates.py in viewlim_to_dt(self)
   1197                              'often happens if you pass a non-datetime '
   1198                              'value to an axis that has datetime units'
-> 1199                              .format(vmin))
   1200         return num2date(vmin, self.tz), num2date(vmax, self.tz)
   1201 

ValueError: view limit minimum -36868.15 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units

所以我希望有人可以帮助我编写更好的代码并解决此问题。我认为matplotlib应该添加一个函数fill_area或类似的函数。

你们对此有何看法?

我有一个熊猫系列,这是图形:我要填充曲线下的区域。问题是调用plt.fill(y)输出:从其他答案中可以看出,这是因为我们需要发送...

python matplotlib fill area curve
2个回答
2
投票

具有这样的功能:matplotlib.pyplot.fill_between()

import matplotlib.pyplot as plt

plt.plot(y, c='red')
plt.fill_between(y.index, y, color='blue', alpha=0.3)

1
投票

看看this

© www.soinside.com 2019 - 2024. All rights reserved.