无法找到引用的已定义小部件,正在创建交互式图表。

huangapple go评论56阅读模式
英文:

Creating an interactive graph but cannot find defined widget referenced

问题

我试图创建一个交互式图表,该图表使用一个数据框(使用第二个函数定义)来进行定义,然后应该使用定义的小部件更新数据框。如果我不使图表具有交互性(即只是运行带有静态值的绘图函数),代码就会正常运行。我已经尝试查看其他一些问题,但似乎找不到我要找的东西(也许我没有提出正确的问题)。

我的原始代码:

%matplotlib inline
style = {'description_width':'initial'}

TR = widgets.BoundedFloatText(value=1734,min=0,max=10000,step=10,description='Top Reservoir (TVDMSL):',style=style,disabled=False)
BR = widgets.BoundedFloatText(value=1771,min=0,max=10000,step=10,description='Base Reservoir (TVDMSL):',style=style,disabled=False)
NGw = widgets.BoundedFloatText(value=0.88,min=0,max=1,step=0.1,description='NTG (fraction):',style=style,disabled=False)
WD = widgets.BoundedFloatText(value=127,min=0,max=10000,step=10,description='Water depth (m):',style=style,disabled=False)
RR = widgets.BoundedFloatText(value=3500,min=0,max=10000,step=100,description='Reservoir radius (m):',style=style,disabled=False)
D = widgets.BoundedFloatText(value=13.2,min=0,max=10000,step=10,description='Depletion (MPa):',style=style,disabled=False)
nuw = widgets.BoundedFloatText(value=0.29,min=0,max=1,step=0.01,description='Poissons Ratio (bar):',style=style,disabled=False)
Ew = widgets.BoundedFloatText(value=7.9,min=0,max=10000,step=1,description='Youngs Modulus (GPa):',style=style,disabled=False)

def plot(BR,TR,NGw,WD,Ew,nuw,RR,e,intv,D):
    
    # run function without print to get the results dataframe
    with HiddenPrints():
        df_results = geertsma(BR,TR,NGw,WD,Ew,nuw,RR,e,intv,D)
    
    # defining x-y data for sea bed and reservoir depth
    Vd_OB = df_results['Vertical displacement (cm)'].iloc[0]
    Vd_UB = df_results['Vertical displacement (cm)'].iloc[-1]
    x = [(Vd_UB-1),(Vd_OB+5)]
    Seabed_y = (WD,WD)
    Res_top_y = (TR,TR)
    Res_base_y = (BR, BR)
    
    # formatting plot
    
    f, ax = plt.subplots(figsize=(5,10))
    ax.set_ylim([0,e])   # setting y plot limit between 0 and depth of interest
    ax.set_xlim(x)   # setting x plot limit
    ax.axhline(y=0, color='darkgrey')    # Adding axis through origin
    ax.axvline(x=0, color='darkgrey')    # Adding axis through origin
    
    # plotting data
    ax.plot(x,Seabed_y,color='steelblue', label='Seabed')    # Plotting seabed level
    ax.plot(x,Res_top_y,color='darkorange', label='Top reservoir')    # plotting top res
    ax.plot(x,Res_base_y,color='gold', label='Base reservoir')      # plotting base res
    ax.plot(df_results['Vertical displacement (cm)'], df_results['Depth'], color='k', label='displacement profile')   # plotting displacement profile
    
    # formatting plot/data
    ax.invert_yaxis()
    ax.tick_params(top=True, labeltop=True, bottom=False, labelbottom=False)
    ax.set_title('Vertical Displacement Profile')
    ax.set_xlabel('Vertical displacement (cm)')
    ax.set_ylabel('Depth (TVDm)')
    ax.xaxis.set_label_position('top')
    ax.legend()

interactive_plot = interactive(plot,{'BR':BR,'TR':TR,'NGw':NGw,'WD':WD,'Ew':Ew,'nuw':nuw,'RR':RR,'e':e,'intv':int,'D':D })
英文:

I'm trying to create an interactive graph which uses a dataframe (defined using a second function) which should then be updated with the defined widgets. The code runs fine if I don't make the plot interactive (i.e. I just run my plot function with static values). I've tried combing through some other questions but can't seem to find what I'm looking for (maybe I'm not asking the right qestions).

My original code:

%matplotlib inline
style = {'description_width':'initial'}
TR = widgets.BoundedFloatText(value=1734,min=0,max=10000,step=10,description='Top Reservoir (TVDMSL):',style=style,disabled=False)
BR = widgets.BoundedFloatText(value=1771,min=0,max=10000,step=10,description='Base Reservoir (TVDMSL):',style=style,disabled=False)
NGw = widgets.BoundedFloatText(value=0.88,min=0,max=1,step=0.1,description='NTG (fraction):',style=style,disabled=False)
WD = widgets.BoundedFloatText(value=127,min=0,max=10000,step=10,description='Water depth (m):',style=style,disabled=False)
RR = widgets.BoundedFloatText(value=3500,min=0,max=10000,step=100,description='Reservoir radius (m):',style=style,disabled=False)
D = widgets.BoundedFloatText(value=13.2,min=0,max=10000,step=10,description='Depletion (MPa):',style=style,disabled=False)
nuw = widgets.BoundedFloatText(value=0.29,min=0,max=1,step=0.01,description='Poissons Ratio (bar):',style=style,disabled=False)
Ew = widgets.BoundedFloatText(value=7.9,min=0,max=10000,step=1,description='Youngs Modulus (GPa):',style=style,disabled=False)
def plot(BR,TR,NGw,WD,Ew,nuw,RR,e,intv,D):
# run function without print to get the results dataframe
with HiddenPrints():
df_results = geertsma(BR,TR,NGw,WD,Ew,nuw,RR,e,intv,D)
# defining x-y data for sea bed and reservoir depth
Vd_OB = df_results['Vertical displacement (cm)'].iloc[0]
Vd_UB = df_results['Vertical displacement (cm)'].iloc[-1]
x = [(Vd_UB-1),(Vd_OB+5)]
Seabed_y = (WD,WD)
Res_top_y = (TR,TR)
Res_base_y = (BR, BR)
# formatting plot
f, ax = plt.subplots(figsize=(5,10))
ax.set_ylim([0,e])   # setting y plot limit between 0 and depth of interest
ax.set_xlim(x)   # setting x plot limit
ax.axhline(y=0, color='darkgrey')    # Adding axis through origin
ax.axvline(x=0, color='darkgrey')    # Adding axis through origin
# plotting data
ax.plot(x,Seabed_y,color='steelblue', label='Seabed')    # Plotting seabed level
ax.plot(x,Res_top_y,color='darkorange', label='Top reservoir')    # plotting top res
ax.plot(x,Res_base_y,color='gold', label='Base reservoir')      # plotting base res
ax.plot(df_results['Vertical displacement (cm)'], df_results['Depth'], color='k', label='displacement profile')   # plotting displacement profile
# formatting plot/data
ax.invert_yaxis()
ax.tick_params(top=True, labeltop=True, bottom=False, labelbottom=False)
ax.set_title('Vertical Displacement Profile')
ax.set_xlabel('Vertical displacement (cm)')
ax.set_ylabel('Depth (TVDm)')
ax.xaxis.set_label_position('top')
ax.legend()
interactive_plot = interactive(plot,{'BR':BR,'TR':TR,'NGw':NGw,'WD':WD,'Ew':Ew,'nuw':nuw,'RR':RR,'e':e,'intv':int,'D':D })

I get the following error message:

ValueError                                Traceback (most recent call last)
Cell In[51], line 58
55 nuw = widgets.BoundedFloatText(value=0.29,min=0,max=1,step=0.01,description='Poissons Ratio (bar):',style=style,disabled=False)
56 Ew = widgets.BoundedFloatText(value=7.9,min=0,max=10000,step=1,description='Youngs Modulus (GPa):',style=style,disabled=False)
---> 58 interactive_plot = interactive(plot,{'BR':BR,'TR':TR,'NGw':NGw,'WD':WD,'Ew':Ew,'nuw':nuw,'RR':RR,'e':e,'intv':int,'D':D })
60 #plot(BR.value,TR.value,NGw.value,WD.value,Ew.value,nuw.value,RR.value,e.value,intv.value,D.value)
61 
62 #out = widgets.interactive_output(plot, {'BR':BR,'TR':TR,'NGw':NGw,'WD':WD,'Ew':Ew,'nuw':nuw,'RR':RR,'e':e,'intv':int,'D':D})
File C:\Appl\my_env\Lib\site-packages\ipywidgets\widgets\interaction.py:172, in interactive.__init__(self, _interactive__interact_f, _interactive__options, **kwargs)
169 self.manual_name = __options.get("manual_name", "Run Interact")
170 self.auto_display = __options.get("auto_display", False)
--> 172 new_kwargs = self.find_abbreviations(kwargs)
173 # Before we proceed, let's make sure that the user has passed a set of args+kwargs
174 # that will lead to a valid call of the function. This protects against unspecified
175 # and doubly-specified arguments.
176 try:
File C:\Appl\my_env\Lib\site-packages\ipywidgets\widgets\interaction.py:272, in interactive.find_abbreviations(self, kwargs)
270     for name, value, default in _yield_abbreviations_for_parameter(param, kwargs):
271         if value is empty:
--> 272             raise ValueError('cannot find widget or abbreviation for argument: {!r}'.format(name))
273         new_kwargs.append((name, value, default))
274 return new_kwargs
ValueError: cannot find widget or abbreviation for argument: 'BR'

答案1

得分: 0

刚刚解决了这个问题。我之前错误地调用了交互式绘图函数,所以函数名相同,但是改成了:

interactive_plot = interactive(plot, {'BR': BR, 'TR': TR, 'NGw': NGw, 'WD': WD, 'Ew': Ew, 'nuw': nuw, 'RR': RR, 'e': e, 'intv': int, 'D': D})

我改成了:

out3 = widgets.interactive_output(plot, {'BR': BR, 'TR': TR, 'NGw': NGw, 'WD': WD, 'Ew': Ew, 'nuw': nuw, 'RR': RR, 'e': e, 'intv': intv, 'D': D})
widgets.VBox([out3])

这样就达到了我想要的效果 无法找到引用的已定义小部件,正在创建交互式图表。

英文:

Just solved this. I was calling interactive plot incorrectly so function was the same but instead of:

interactive_plot = interactive(plot,{'BR':BR,'TR':TR,'NGw':NGw,'WD':WD,'Ew':Ew,'nuw':nuw,'RR':RR,'e':e,'intv':int,'D':D })

I used:

out3 = widgets.interactive_output(plot,{'BR':BR,'TR':TR,'NGw':NGw,'WD':WD,'Ew':Ew,'nuw':nuw,'RR':RR,'e':e,'intv':intv,'D':D })
widgets.VBox([out3])

which worked as I wanted 无法找到引用的已定义小部件,正在创建交互式图表。

huangapple
  • 本文由 发表于 2023年7月10日 21:11:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定