与matplotlib fill_between()和>符号有关的问题

问题描述 投票:1回答:1

我正在尝试将matplotlib中的fill_between()函数用于正在制作的图形。我使用了文档中的确切代码(https://matplotlib.org/3.2.1/gallery/lines_bars_and_markers/fill_between_demo.html#sphx-glr-gallery-lines-bars-and-markers-fill-between-demo-py),但是当我使用它时,出现以下错误:

fig, ax = plt.subplots(figsize=(16,8))
y1 = sns.lineplot('game_seconds_remaining', 'home_wp', data=vb, color='#4F2683',linewidth=2)
y2 = sns.lineplot('game_seconds_remaining', 'away_wp', data=vb, color='#FB4F14',linewidth=2)

x = plt.axhline(y=.50, color='white', alpha=0.7)

ax.fill_between(x, y1, y2, where=(y1 > x), color='C0', alpha=0.3, interpolate=True)

Output: TypeError: '>' not supported between instances of 'AxesSubplot' and 'AxesSubplot'

为什么这对我不起作用,但对文档有效?我要做的是遮盖水平线(x)下方和上方的任何区域。任何帮助是极大的赞赏。谢谢!

python pandas dataframe matplotlib seaborn
1个回答
1
投票

使用虚拟数据:

# dummy dataframe
x = np.linspace(0,2*np.pi, 100)

df= pd.DataFrame({
    'a': x,
    'b': np.cos(x),
    'c': np.sin(x), 
})

fig = plt.figure()
ax=fig.add_subplot(111)
sns.lineplot('a', 'b', data=df, ax=ax, label='b')
sns.lineplot('a', 'c', data=df, ax=ax, label='c')

ax.fill_between(df['a'], 0.5, df['b'], where=df['b']>.5)
ax.fill_between(df['a'], 0.5, df['c'], where=df['c']>.5)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.