我有三条曲线,我想在它们之间填充:
我正在使用以下代码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
a=np.arange(-5,300,2)
def f(a): return a**2
def slope(a):
slope=2*(a)
return slope
xp = 60
yp = f(xp)
def line(a, xp, yp):
return slope(xp)*(a - xp) + yp
plt.plot(a, f(a))
plt.scatter(xp, yp, color='C1', s=50)
plt.plot(a, line(a, xp, yp), 'C1--', linewidth = 2)
xp1=250
yp1=f(xp1)
plt.scatter(xp1, yp1, color='C1', s=50)
plt.plot(a, line(a, xp1, yp1), 'C3--', linewidth = 2)
plt.fill_between(a,f(a),line(a, xp1, yp1),line(a, xp, yp),color='green')
plt.show()
输出为:
我想要一个填充限制,它是yp1
和yp
。
我尝试在fillbetween
命令中使用where
参数,如下所示:
plt.fill_between(a,f(a),line(a, xp1, yp1),line(a, xp, yp),where=[(a>yp)and (a<yp1) for a in a],color='green')
但是由于某种原因,我得到了这个错误:TypeError: fill_between() got multiple values for argument 'where'
有什么帮助或想法吗
错误
TypeError: fill_between() got multiple values for argument 'where'
源于您提供了两次where
,因为fill_between的签名是Axes.fill_between(self, x, y1, y2=0, where=None, ...)
。在代码中,line(a, xp, yp)
作为where
提供除了使用
where=(a > yp) & (a < yp1)
限制两点之间的填充区域外,您实际上要做的是仔细选择y1
和y2
限制我已经把变量的名称改了一点,使之更具可读性。数组以大写字母开头,单个值为小写
相关问题 更多 >
编程相关推荐