我正在try 使用matplotlib填充两条相交线下方和两条线上方的区域.我可以在两行之间填充,但还没有找到一种简单的方法来反转之前获得的区域.我唯一的解决办法是创建一些额外的函数(底部的一个低的,最小的一个,顶部的类似功能),这有点麻烦,需要手动输入(见下文).有更好的解决方案吗?
import numpy as np
import matplotlib.pyplot as plt
# Doesn't work
def f1(x): return 32.0 * x + 2.0
def f2(x): return -55.0 * x
xRng=[-1, 1]
plt.plot(xRng, [f1(x) for x in xRng], 'b-')
plt.plot(xRng, [f2(x) for x in xRng], 'r-')
plt.fill_between(xRng, [f1(x) for x in xRng], [f2(x) for x in xRng], color='g') # Would like the fill inverted
plt.title('Not good'); plt.show()
# Works, but clumsy
def fLo(x): return -100
def fHi(x): return 100
def fMin(x): return min(f1(x), f2(x))
def fMax(x): return max(f1(x), f2(x))
xRng=np.linspace(-1, 1, 100)
plt.plot(xRng, [f1(x) for x in xRng], 'b-')
plt.plot(xRng, [f2(x) for x in xRng], 'r-')
plt.fill_between(xRng, [fMin(x) for x in xRng], [fLo(x) for x in xRng], color='g')
plt.fill_between(xRng, [fMax(x) for x in xRng], [fHi(x) for x in xRng], color='g')
plt.title('Complicated'); plt.show()
编辑:按照@Mad物理学家的建议交换BG和FG colored颜色 将在基本情况下有效,但如果有几个这样的区域要叠加则不起作用