1

我正在尝试在类中使用面板小部件来更改绘图。下面的示例输出一次绘图,但是当我从小部件中选择不同的变量时,它不会更新绘图。

import pandas as pd
import param

import holoviews as hv
hv.extension('bokeh')

import panel as pn
pn.extension()

df = pd.DataFrame({'index':[1,2,3,4,5],
                   'signal1':[1,2,3,4,5],
                   'signal2':[5,4,3,2,1]}).set_index('index')

class Plot(param.Parameterized):
    def __init__(self, df):
        self.multi_select = pn.widgets.MultiSelect(name='Vars', value=['signal1'],
                                                   options=['signal1', 'signal2'])
        self.df = df
        self.watcher = self.multi_select.param.watch(self.dashboard, 'value')
        super().__init__()

    def dashboard(self, *events, **kwargs):
        self.plt = hv.Curve(self.df, [self.df.index.name, self.multi_select.value[0]])
        return self.plt

a = Plot(df)
pn.Row(a.multi_select,a.dashboard)

但是,如果我使用参数而不是面板小部件,它会按预期工作。

class Plot2(param.Parameterized):
    multi_select = param.ListSelector(default=['signal1'],
                                      objects=['signal1', 'signal2'])

    df = df

    def dashboard(self, **kwargs):
        self.plt = hv.Curve(self.df, [self.df.index.name, self.multi_select[0]])
        return self.plt

b = Plot2()
pn.Row(b.param,b.dashboard)

我做错了什么还是根本不可能?谢谢

4

1 回答 1

2

首先我修改了你的代码。主要变化是:我添加了

  • Plot.dashboard()在事件和变量上带有打印语句的 try-except 子句self.multi_select
  • 使用外部散景服务器view = pn.Row(a.multi_select,a.dashboard)view.app()从 try-except-clause 获取打印。

完整的情节():

class Plot(param.Parameterized):

    def __init__(self, df, **params):
        super(Plot, self).__init__(**params)
        self.multi_select = pn.widgets.MultiSelect(name='Vars', value=['signal1'],
                                                   options=['signal1', 'signal2'])
        self.df = df
        self.watcher = self.multi_select.param.watch(self.dashboard, 'value')


    def dashboard(self, *events, **kwargs):
        try: 
            event = events[0] # get first event in events
            select = events[0].obj # obj is our multi_select

            # see, what event looks like, and if select and self.multi_select are the same
            print(event, '\n', select, '\n', self.multi_select)

        except IndexError: pass

        self.plt = hv.Curve(self.df, [self.df.index.name, self.multi_select.value[0]])
        return self.plt

a = Plot(df)
view = pn.Row(a.multi_select,a.dashboard)
view.app()

在此处输入图像描述

因此,您的代码按预期工作,但未显示更改。

第二:将曲线图与多选分开并玩弄显示,如果方法view取决于更改的参数(这里是用于绘图的列的名称),则曲线只会更改:

class Curve_(param.Parameterized):
    col = param.String()

    def __init__(self, df, col, **params):
        super(Curve_, self).__init__(**params)
        self.df = df
        self.col = col

    @param.depends('col', watch=True)
    def view(self): 
        return hv.Curve(self.df, [self.df.index.name, self.col])

t=Curve_(df, 'signal1')
pn.Column(t.view)

结合这些类PlotCurve_使用参数dashboard_trigger来保持仪表板更新,现在一切都按预期工作。

class Plot(param.Parameterized):
    dashboard_trigger = param.Number(1)

    def __init__(self, df, **params):
        super(Plot, self).__init__(**params)
        self.multi_select = pn.widgets.MultiSelect(name='Vars', value=['signal1'],
                                                   options=['signal1', 'signal2'])
        self.curve = Curve_(df, self.multi_select.value[0])
        self.watcher = self.multi_select.param.watch(self.set_column, 'value')

    def set_column(self, *events):
        self.curve.col = events[0].obj.value[0]
        self.dashboard_trigger = self.dashboard_trigger + 1

    @param.depends('dashboard_trigger', watch=True)
    def dashboard(self, *events, **kwargs):
        return self.curve.view

a = Plot(df)
pn.Row(a.multi_select,a.dashboard)

在此处输入图像描述

于 2019-05-03T09:32:38.587 回答