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

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

Creating an interactive graph but cannot find defined widget referenced

问题

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

我的原始代码:

  1. %matplotlib inline
  2. style = {'description_width':'initial'}
  3. TR = widgets.BoundedFloatText(value=1734,min=0,max=10000,step=10,description='Top Reservoir (TVDMSL):',style=style,disabled=False)
  4. BR = widgets.BoundedFloatText(value=1771,min=0,max=10000,step=10,description='Base Reservoir (TVDMSL):',style=style,disabled=False)
  5. NGw = widgets.BoundedFloatText(value=0.88,min=0,max=1,step=0.1,description='NTG (fraction):',style=style,disabled=False)
  6. WD = widgets.BoundedFloatText(value=127,min=0,max=10000,step=10,description='Water depth (m):',style=style,disabled=False)
  7. RR = widgets.BoundedFloatText(value=3500,min=0,max=10000,step=100,description='Reservoir radius (m):',style=style,disabled=False)
  8. D = widgets.BoundedFloatText(value=13.2,min=0,max=10000,step=10,description='Depletion (MPa):',style=style,disabled=False)
  9. nuw = widgets.BoundedFloatText(value=0.29,min=0,max=1,step=0.01,description='Poissons Ratio (bar):',style=style,disabled=False)
  10. Ew = widgets.BoundedFloatText(value=7.9,min=0,max=10000,step=1,description='Youngs Modulus (GPa):',style=style,disabled=False)
  11. def plot(BR,TR,NGw,WD,Ew,nuw,RR,e,intv,D):
  12. # run function without print to get the results dataframe
  13. with HiddenPrints():
  14. df_results = geertsma(BR,TR,NGw,WD,Ew,nuw,RR,e,intv,D)
  15. # defining x-y data for sea bed and reservoir depth
  16. Vd_OB = df_results['Vertical displacement (cm)'].iloc[0]
  17. Vd_UB = df_results['Vertical displacement (cm)'].iloc[-1]
  18. x = [(Vd_UB-1),(Vd_OB+5)]
  19. Seabed_y = (WD,WD)
  20. Res_top_y = (TR,TR)
  21. Res_base_y = (BR, BR)
  22. # formatting plot
  23. f, ax = plt.subplots(figsize=(5,10))
  24. ax.set_ylim([0,e]) # setting y plot limit between 0 and depth of interest
  25. ax.set_xlim(x) # setting x plot limit
  26. ax.axhline(y=0, color='darkgrey') # Adding axis through origin
  27. ax.axvline(x=0, color='darkgrey') # Adding axis through origin
  28. # plotting data
  29. ax.plot(x,Seabed_y,color='steelblue', label='Seabed') # Plotting seabed level
  30. ax.plot(x,Res_top_y,color='darkorange', label='Top reservoir') # plotting top res
  31. ax.plot(x,Res_base_y,color='gold', label='Base reservoir') # plotting base res
  32. ax.plot(df_results['Vertical displacement (cm)'], df_results['Depth'], color='k', label='displacement profile') # plotting displacement profile
  33. # formatting plot/data
  34. ax.invert_yaxis()
  35. ax.tick_params(top=True, labeltop=True, bottom=False, labelbottom=False)
  36. ax.set_title('Vertical Displacement Profile')
  37. ax.set_xlabel('Vertical displacement (cm)')
  38. ax.set_ylabel('Depth (TVDm)')
  39. ax.xaxis.set_label_position('top')
  40. ax.legend()
  41. 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:

  1. %matplotlib inline
  2. style = {'description_width':'initial'}
  3. TR = widgets.BoundedFloatText(value=1734,min=0,max=10000,step=10,description='Top Reservoir (TVDMSL):',style=style,disabled=False)
  4. BR = widgets.BoundedFloatText(value=1771,min=0,max=10000,step=10,description='Base Reservoir (TVDMSL):',style=style,disabled=False)
  5. NGw = widgets.BoundedFloatText(value=0.88,min=0,max=1,step=0.1,description='NTG (fraction):',style=style,disabled=False)
  6. WD = widgets.BoundedFloatText(value=127,min=0,max=10000,step=10,description='Water depth (m):',style=style,disabled=False)
  7. RR = widgets.BoundedFloatText(value=3500,min=0,max=10000,step=100,description='Reservoir radius (m):',style=style,disabled=False)
  8. D = widgets.BoundedFloatText(value=13.2,min=0,max=10000,step=10,description='Depletion (MPa):',style=style,disabled=False)
  9. nuw = widgets.BoundedFloatText(value=0.29,min=0,max=1,step=0.01,description='Poissons Ratio (bar):',style=style,disabled=False)
  10. Ew = widgets.BoundedFloatText(value=7.9,min=0,max=10000,step=1,description='Youngs Modulus (GPa):',style=style,disabled=False)
  11. def plot(BR,TR,NGw,WD,Ew,nuw,RR,e,intv,D):
  12. # run function without print to get the results dataframe
  13. with HiddenPrints():
  14. df_results = geertsma(BR,TR,NGw,WD,Ew,nuw,RR,e,intv,D)
  15. # defining x-y data for sea bed and reservoir depth
  16. Vd_OB = df_results['Vertical displacement (cm)'].iloc[0]
  17. Vd_UB = df_results['Vertical displacement (cm)'].iloc[-1]
  18. x = [(Vd_UB-1),(Vd_OB+5)]
  19. Seabed_y = (WD,WD)
  20. Res_top_y = (TR,TR)
  21. Res_base_y = (BR, BR)
  22. # formatting plot
  23. f, ax = plt.subplots(figsize=(5,10))
  24. ax.set_ylim([0,e]) # setting y plot limit between 0 and depth of interest
  25. ax.set_xlim(x) # setting x plot limit
  26. ax.axhline(y=0, color='darkgrey') # Adding axis through origin
  27. ax.axvline(x=0, color='darkgrey') # Adding axis through origin
  28. # plotting data
  29. ax.plot(x,Seabed_y,color='steelblue', label='Seabed') # Plotting seabed level
  30. ax.plot(x,Res_top_y,color='darkorange', label='Top reservoir') # plotting top res
  31. ax.plot(x,Res_base_y,color='gold', label='Base reservoir') # plotting base res
  32. ax.plot(df_results['Vertical displacement (cm)'], df_results['Depth'], color='k', label='displacement profile') # plotting displacement profile
  33. # formatting plot/data
  34. ax.invert_yaxis()
  35. ax.tick_params(top=True, labeltop=True, bottom=False, labelbottom=False)
  36. ax.set_title('Vertical Displacement Profile')
  37. ax.set_xlabel('Vertical displacement (cm)')
  38. ax.set_ylabel('Depth (TVDm)')
  39. ax.xaxis.set_label_position('top')
  40. ax.legend()
  41. 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:

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

答案1

得分: 0

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

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

我改成了:

  1. 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})
  2. widgets.VBox([out3])

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

英文:

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

  1. 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:

  1. 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 })
  2. 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:

确定