如何在python中使用fill_between()?
我有一个图形,希望填充图形与某个值相交的背景。(在我的示例中为0.75) 我使用以下代码:如何在python中使用fill_between()?,python,pandas,dataframe,matplotlib,Python,Pandas,Dataframe,Matplotlib,我有一个图形,希望填充图形与某个值相交的背景。(在我的示例中为0.75) 我使用以下代码: fig, ax = plt.subplots() df2 = pd.read_csv('kcfsinew2.csv') x=df2['Date'] y=df2['KCFSI'] ax.plot(x, y , color='black') theta = 0.75 ax.axhline(theta, color='grey', lw=2, alpha=0.5) ax.fill_between(x ,
fig, ax = plt.subplots()
df2 = pd.read_csv('kcfsinew2.csv')
x=df2['Date']
y=df2['KCFSI']
ax.plot(x, y , color='black')
theta = 0.75
ax.axhline(theta, color='grey', lw=2, alpha=0.5)
ax.fill_between(x , -1 , 6, where= y > theta ,facecolor='grey', alpha=0.5, transform=trans)
我得到的图像没有给我预期的输出,我不知道我的代码出了什么问题
y2=0.75
以显示图形底部的位置where=df.y>0.75
,使其仅填充0.75以上interpolate=True
,使其在每次线交叉时填充import pandas as pd # '0.23.4'
import numpy as np # '1.15.1'
import matplotlib # '2.2.3'
import matplotlib.pyplot as plt
import seaborn as sns; sns.set() # '0.9.0'
# set seed
np.random.seed(1)
# make df
df = pd.DataFrame(np.random.uniform(-1, 3, (30,)), index=pd.DatetimeIndex(start='20180101', periods=30, freq='3D'), columns=['y'])
# figure
fig, ax = plt.subplots()
# plot
df.plot(figsize=(16,8), ax=ax)
# draw line
ax.axhline(0.75, color='k')
# annotate
ax.text(0, 0.75, '0.75', transform=ax.get_yaxis_transform(), ha='right', )
# fill between y=0.75 and df.y
ax.fill_between(x=df.index, y1=df.y, y2=0.75, where=df.y > 0.75, interpolate=True)
plt.show()
代码中的
y
是什么?只需编辑我的帖子并添加y。尝试用θ替换第二个和第三个参数:ax.fill\u(x,θ,其中=y>θ,…
@Dennis请将您的问题转换为a。对于您的特定代码,请使用一些虚构的数据替换csv
文件,这样每个人都可以通过复制/粘贴来运行您的示例。除非您提供可运行的代码,否则读者在这里很难帮助您。