简体   繁体   English

Matplotlib fill_between()解决方法

[英]Matplotlib fill_between() workaround

I have a strange situation which I would like a bit of assistance. 我遇到一个奇怪的情况,需要一点帮助。 I have the following data: 我有以下数据:

xdata = [ 11.125,  11.375,  11.625,  11.875,  12.125,  12.375,  12.625,  12.875,  13.125, 13.375,  13.625,  13.875,  14.125,  14.375,  14.625,  14.875,  15.125,  15.375]
ydata = [ 5.49305494,  6.51732366,  6.54733551,  6.38045781,  6.16101383,  5.93700054,  5.70674253,  5.47409529,  5.23715401,  4.98422568,  4.72124987,  4.43762374,   4.11756956,  3.74888544,  3.32613096,  2.79169065,  2.0374265,   1.07918125]

What I would like is to use the fill_between() function, such that the filled region is between two x-values which are not part of the list xdata, while at the same time fills the region between y=0, and the curve generated by the ydata provided. 我想要的是使用fill_between()函数,以使填充区域位于两个x值之间,这两个x值不属于列表xdata,而同时填充y = 0之间的区域,并生成曲线通过提供的ydata。

I've thought of two things: 1) Not caring about being bounded by the y-data (in which case I would use the axvspan() function); 我想到了两件事:1)不关心被y数据限制(在这种情况下,我将使用axvspan()函数); this is no longer really an option, and 2) doing some sort of interpolation scheme in order to find the interpolated ydata values for which the x values I have (which again are not part of xdata). 这不再是一个真正的选择,并且2)执行某种插值方案以便找到我拥有的x值(它们又不是xdata的一部分)的插值ydata值。 If I do move forward with the second idea, I would need to know how matplotlib interpolates between data points by default when using the plot() function in order to try to match the curve generated by the ydata exactly. 如果我确实提出第二个想法,我将需要知道在使用plot()函数时,默认情况下matplotlib如何在数据点之间进行插值,以尝试完全匹配ydata生成的曲线。

I'm open to the interpolation idea, but I'm really open to anything that works. 我对插值方法持开放态度,但实际上对任何可行的方法都持开放态度。 Thanks! 谢谢!

I think you probably have to do interpolation. 我认为您可能必须进行插值。 Even somehow "cropping" the result from 甚至以某种方式“裁剪”来自

plt.fill_between(xdata, 0, ydata)

未裁剪

As discussed in the comments above would be equivalent to linear interpolation (straight lines between each data point). 如以上注释中所述,等效于线性插值(每个数据点之间的直线)。 Here's how you could do it: 您可以按照以下方式进行操作:

xdata = ...
ydata = ...
xleft, xright = 13.3979400087, 13.414973348
xfill = np.linspace(xleft, xright)
yfill = np.interp(xfill, xdata, ydata)
plt.fill_between(xfill, 0, yfill, color='r')

If you do this on top of the original, you can see it better: 如果您在原件上进行此操作,则可以更好地看到它: 线性

Of course, you could do fancier interpolation, with a spline being the next step: 当然,您可以进行更高级的插值,下一步是样条线:

from scipy import interpolate
# same as above ...
yfill_spline = interpolate.spline(xdata, ydata, xfill) #note the different args ordering from np.interp
plt.fill_between(xfill, 0, yfill_spline, color='g')

The difference is pretty subtle for your example so I've zoomed in to the top edge of the filled region, but with higher curvature data you'll notice a difference more easily: 对于您的示例,差异非常细微,因此我放大了填充区域的顶部边缘,但是使用较高的曲率数据,您会更容易注意到差异: 线性和样条

For comparison, see the uncropped version with linear vs spline interpolation. 为了进行比较,请参见具有线性和样条插值的非裁剪版本。 You'd notice a big difference between the methods if you had xleft and xright near the peak ( 1112 or so). 如果xleftxright在峰值附近( 1112左右),您会发现这两种方法之间存在很大差异。 线性和样条线,未裁剪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM