【问题标题】:How to set widgets to link to array for jupyternotebooks如何设置小部件以链接到 jupyter 笔记本的数组
【发布时间】:2020-05-06 18:37:24
【问题描述】:

我正在尝试设置一个交互式笔记本来绘制一些插值 GPS 数据。我有自己的绘图工作,但我正在尝试使用 ipython 小部件使其对其他人更具交互性。

目前,我的绘图是这样的

def create_grid(array,spacing=.01):
    '''
    creates evenly spaced grid from the min and max of an array
    '''
    grid = np.arange(np.amin(array), np.amax(array),spacing)
    return grid
def interpolate(x, y, z, grid_spacing = .01, model='spherical',returngrid = False):
    '''Interpolates z value and uses create_grid to create a grid of values based on min and max of x and y'''
    grid_x = create_grid(x,spacing = grid_spacing)
    grid_y = create_grid(y, spacing = grid_spacing)
    OK = OrdinaryKriging(x, y, z, variogram_model=model, verbose = False,\
                    enable_plotting=False, nlags = 20)
    z1, ss1 = OK.execute('grid', grid_x,grid_y,mask = False)
    print('Interpolation Complete')
    vals=np.ma.getdata(z1)
    sigma = np.ma.getdata(ss1)
    if returngrid == False:
        return vals,sigma
    else:
        return vals, sigma, grid_x, grid_y

mesh_x, mesh_y = np.meshgrid(grid_x,grid_y)
plot = plt.scatter(mesh_x, mesh_y, c = z1, cmap = cm.hsv)
cb = plt.colorbar(plot)
cb.set_label('Northing Change')

plt.show()

'''

这目前有效,但我正在尝试设置一个小部件来更改克里金插值中的变异函数模型,以及更改要插值的字段。

目前,我有:

def update_plot(zfield,variogram):
    plt.clf()
    z1, ss1, grid_x,grid_y =interpolate(lon,lat,zfield,returngrid= True,model=variogram)
    mesh_x, mesh_y = np.meshgrid(grid_x,grid_y)
    plot = plt.scatter(mesh_x, mesh_y, c = z1, cmap = cm.hsv)
    cb = plot.colorbar(plot)
    cb.set_label('Interpolated Value')


variogram = widgets.Dropdown(options = ['linear',  'power',  'gaussian', 'spherical',  'exponential',  'hole-effect'],
                            value = 'spherical', description = "Variogram model for interpolation")
zfield = widgets.Dropdown(options = {'Delta N':delta_n, 'Delta E': delta_e,'Delta V':delta_v},value = 'Delta N', 
                          description = 'Interpolated value')
widgets.interactive(update_plot, variogram = variogram,zfield =zfield)

这会导致错误

TraitError:无效选择:未找到值

值 delta_n、delta_e 和 delta_v 是 numpy 数组。我试过查看文档,但它不像 matplotlibs 文档之类的那样详细,所以我觉得我在这里有点盲目。

谢谢

【问题讨论】:

    标签: python jupyter-notebook ipython interpolation ipywidgets


    【解决方案1】:

    在这一行中,您将 Dropdown 的可能值指定为:

    zfield = widgets.Dropdown(options = {'Delta N':delta_n, 'Delta E': delta_e,'Delta V':delta_v}
    

    当使用映射时,dict 的值被解释为可能的选项。所以value = 'Delta N' 会导致错误,因为这不是 Dropdown 的可能值之一(尽管它是映射字典中的键之一)。我相信你想要的是value = delta_n。

    【讨论】:

    • 执行此操作时会出现同样的错误。我还尝试将 dict 放入变量“vals_dict”,然后调用“value = vals_dict.values()”,我得到了同样的错误。
    猜你喜欢
    • 1970-01-01
    • 2018-11-22
    • 2017-10-01
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 2017-07-04
    相关资源
    最近更新 更多