【发布时间】:2021-03-02 21:33:25
【问题描述】:
我有这个图,我想在它们之间填充三条曲线:
我正在使用此代码:
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'
有什么帮助或想法吗?
【问题讨论】:
标签: python python-3.x pandas matplotlib google-colaboratory