Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:在三条曲线之间填充_Python_Python 3.x_Pandas_Matplotlib_Google Colaboratory - Fatal编程技术网

Python:在三条曲线之间填充

Python:在三条曲线之间填充,python,python-3.x,pandas,matplotlib,google-colaboratory,Python,Python 3.x,Pandas,Matplotlib,Google Colaboratory,我有三条曲线,我想在它们之间填充: 我正在使用以下代码: 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(x

我有三条曲线,我想在它们之间填充:

我正在使用以下代码:

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')

plt.fill_-between(a,f(a),line(a,xp1,yp1),line(a,xp,yp),其中=[(a>yp)和(aerror
TypeError:fill_-between()为参数“where”
获取了多个值,因为您提供了两次
where
,因为轴的签名是
Axes.fill_-between(self,x,y1,y2=0,where=None,…)
。在您的代码中,
行(a,xp,yp)
作为
提供,其中

除了使用
where=(a>yp)和(a
限制两点之间的填充区域外,您实际上要做的是仔细选择
y1
y2
限制

我把变量名改了一点,使之更具可读性。数组以大写字母开头,单个值为小写

import numpy as np
import matplotlib.pyplot as plt

def f(X):
    return X**2

def slope(x):
  return 2*x

def line(X, xp, yp):
    return slope(xp)*(X - xp) + yp

fig, ax = plt.subplots()
X = np.arange(-5, 300, 2)

Y0 = f(X)
ax.plot(X, Y0)

x_left = 60
y_left = f(x_left)
Y1 = line(X, x_left, y_left)
ax.scatter(x_left, y_left, color='C1', s=50)
ax.plot(X, Y1, 'C1--', linewidth = 2)

x_right = 250
y_right = f(x_right)
Y2 = line(X, x_right, y_right)
ax.scatter(x_right, y_right, color='C1', s=50)
ax.plot(X, Y2, 'C3--', linewidth = 2)

where = (x_left < X) & (X < x_right)
Ylower = [max(y1, y2) for (y1, y2) in zip(Y1, Y2)]
ax.fill_between(X, Ylower, Y0, where=where, color='green')

fig.show()
将numpy导入为np
将matplotlib.pyplot作为plt导入
def f(X):
返回X**2
def坡度(x):
返回2*x
def管路(X、xp、yp):
返回斜率(xp)*(X-xp)+yp
图,ax=plt.子批次()
X=np.arange(-5300,2)
Y0=f(X)
ax.图(X,Y0)
左x_=60
y_左=f(x_左)
Y1=线(X,X_左,y_左)
最大散射(x_左,y_左,颜色=C1',s=50)
坐标图(X,Y1,'C1--',线宽=2)
x_右=250
y_right=f(x_right)
Y2=直线(X,X_右,y_右)
最大散射(x_右,y_右,color='C1',s=50)
坐标图(X,Y2,'C3--',线宽=2)
式中=(x_左
错误
TypeError:fill\u between()为参数'where'
获取了多个值,这是因为您提供了两次
where
,因为is
轴的签名。fill\u between(self,x,y1,y2=0,where=None,…)
。在您的代码中,
行(a,xp,yp)
作为
where
提供

除了使用
where=(a>yp)和(a
限制两点之间的填充区域外,您实际上要做的是仔细选择
y1
y2
限制

我把变量名改了一点,使之更具可读性。数组以大写字母开头,单个值为小写

import numpy as np
import matplotlib.pyplot as plt

def f(X):
    return X**2

def slope(x):
  return 2*x

def line(X, xp, yp):
    return slope(xp)*(X - xp) + yp

fig, ax = plt.subplots()
X = np.arange(-5, 300, 2)

Y0 = f(X)
ax.plot(X, Y0)

x_left = 60
y_left = f(x_left)
Y1 = line(X, x_left, y_left)
ax.scatter(x_left, y_left, color='C1', s=50)
ax.plot(X, Y1, 'C1--', linewidth = 2)

x_right = 250
y_right = f(x_right)
Y2 = line(X, x_right, y_right)
ax.scatter(x_right, y_right, color='C1', s=50)
ax.plot(X, Y2, 'C3--', linewidth = 2)

where = (x_left < X) & (X < x_right)
Ylower = [max(y1, y2) for (y1, y2) in zip(Y1, Y2)]
ax.fill_between(X, Ylower, Y0, where=where, color='green')

fig.show()
将numpy导入为np
将matplotlib.pyplot作为plt导入
def f(X):
返回X**2
def坡度(x):
返回2*x
def管路(X、xp、yp):
返回斜率(xp)*(X-xp)+yp
图,ax=plt.子批次()
X=np.arange(-5300,2)
Y0=f(X)
ax.图(X,Y0)
左x_=60
y_左=f(x_左)
Y1=线(X,X_左,y_左)
最大散射(x_左,y_左,颜色=C1',s=50)
坐标图(X,Y1,'C1--',线宽=2)
x_右=250
y_right=f(x_right)
Y2=直线(X,X_右,y_右)
最大散射(x_右,y_右,color='C1',s=50)
坐标图(X,Y2,'C3--',线宽=2)
式中=(x_左