Python 使用Matplotlib在两行之间填充,有2个限制

Python 使用Matplotlib在两行之间填充,有2个限制,python,matplotlib,Python,Matplotlib,我想填充曲线y1=x^3和线y2=3x-2之间的区域。 下面是我的代码,但是,我想把限制放在y11的曲线之间填充 我的代码: import matplotlib.pyplot as plot import numpy as np x = np.linspace(-2.5, 2.5, 100) y1 = np.array([i**3 for i in x]) y2 = np.array([3*i-2 for i in x]) fig = plot.figure() ax = fig.add

我想填充曲线y1=x^3和线y2=3x-2之间的区域。 下面是我的代码,但是,我想把限制放在y11的曲线之间填充

我的代码:

import matplotlib.pyplot as plot
import numpy as np

x = np.linspace(-2.5, 2.5, 100)
y1 = np.array([i**3 for i in x]) 
y2 = np.array([3*i-2 for i in x]) 

fig = plot.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x, y1, label=r"$y=x^3$")
ax.plot(x, y2, label=r"$y=3x-2$")
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.fill_between(x, y1, y2, where=y2<y1, facecolor='green')
ax.legend()

plot.show()
将matplotlib.pyplot导入为绘图
将numpy作为np导入
x=np.linspace(-2.5100)
y1=np.数组([i**3表示x中的i])
y2=np.数组([3*i-2表示x中的i])
图=绘图。图()
ax=图add_子批次(1,1,1)
ax.绘图(x,y1,标签=r“$y=x^3$”)
轴图(x,y2,标签=r“$y=3x-2$”)
斧头刺[‘左’]。设置_位置(‘中心’)
斧头刺['right'].设置颜色('none')
斧头刺['bottom'].设置_位置('center'))
斧头刺['top'].设置颜色(“无”)
ax.spines['left'].设置智能边界(True)
ax.spines['bottom'].设置智能边界(True)
ax.xaxis.set\u ticks\u位置('底部')
ax.yaxis.set\u ticks\u位置(“左”)

ax.fill_在(x,y1,y2,其中=y2之间。最简单的解决方法是定义3个新变量,u,v和w,其中u保存v和w的x值,v=x^3,w=3x-2

u=x[x<1]
v=y1[y1<1]
w=y2[y2<1]
u=x[x
ax.fill_between(u, v, w, where=w<v, facecolor='green')