matplotlib和fill_between和fill_betweenx有所不同

use*_*577 3 python matplotlib

我不明白如何fill_betweenx()在matplotlib中使用。fill_between()有何不同?阅读fill_betweenx()我的文档后,我尝试实现它:

x=np.linspace(0,2*3.14,50)

y=np.sin(x)

plt.figure(figsize=(10,5))

plt.fill_betweenx(y,2,3,color='b')

plt.plot(x,y)
Run Code Online (Sandbox Code Playgroud)

根据我的理解,它应该用蓝色填充x = 2和x = 3之间的正弦曲线,但是我得到了:

谁能向我解释为什么没有填写?

Imp*_*est 5

似乎您想填充正弦曲线,例如在y = 0和正弦之间。您可以使用将此填充限制为x坐标的范围where

import matplotlib.pyplot as plt
import numpy as np

x=np.linspace(0,2*3.14,50)
y=np.sin(x)

plt.fill_between(x,y,0,where=(x>2) & (x<=3),color='b')
plt.plot(x,y)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

相反,fill_betweenx如果要在x方向上的曲线之间填充,则可以使用。例如

plt.fill_betweenx(x,y,where=(x>2) & (x<=3), color='b')
plt.plot(y,x)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明