matplotlib 正在创建交互式图形,但无法找到引用的已定义微件

yqlxgs2m  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(95)

我试图创建一个交互式图形,它使用一个 Dataframe (使用第二个函数定义),然后应该用定义的小部件更新。如果我不使绘图具有交互性(即,我只是用静态值运行我的plot函数)。我试着梳理了一些其他的问题,但似乎找不到我要找的东西(也许我没有问正确的问题)。
我的原始代码:

%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 })

字符串
我收到以下错误消息:

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'

2jcobegt

2jcobegt1#

刚刚解决了这个。我错误地调用了交互式绘图,因此函数是相同的,但不是:

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])


正如我所想的那样工作:)

相关问题