简体   繁体   中英

Python matplotlib fill between

Given the four curves in the image below, I am trying to fill everything above the top curve in red. My code below:

# Fitting curve IV
popt, _ = curve_fit(exponenial_func,xD, divider(yD), p0=(1,1,1), maxfev=10000)
expZeroFitC = exponenial_func(x, *popt)
plt.plot(x, expZeroFitC, label='Curve IV', color='#000000')

alpha=1
plt.fill_between(x, expZeroFit, expZeroFitA, color='#4CFF33', alpha=alpha)
plt.fill_between(x, expZeroFitA, expZeroFitB, color='#F7E508', alpha=alpha)
plt.fill_between(x, expZeroFitB, expZeroFitC, color='#FFC300', alpha=alpha)

d = scipy.zeros(len(expZeroFit))

plt.fill_between(x, expZeroFit, where=expZeroFit>=d, interpolate=True, color='#3ECB1E')

d = scipy.zeros(len(expZeroFitC))

plt.fill_between(x, expZeroFitC, where=expZeroFitC<=d, interpolate=True, color='#DA3716')

The fill_between call works well to color everything below expZeroFit in green, but seems to fail in coloring everything above expZeroFitC in red.

Any advice?

在此处输入图片说明

Bit of a hard one when I'm just teaching myself matplotlib but it makes the learning it a bit more interesting.

But i think the problem answer is found in the definition of the how were functions in fill_between found here which says

"where : array of bool (length N), optional, default: None

Define where to exclude some horizontal regions from being filled. The filled regions are defined by the coordinates x[where]. More precisely, fill between x[i] and x[i+1] if where[i] and where[i+1]. Note that this definition implies that an isolated True value between two False values in where will not result in filling. Both sides of the True position remain unfilled due to the adjacent False values."

So a false value results in the not filling of the red. So I would suggest looking at another way to fill in the space above expZeroFitC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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