displays.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:triflow 作者: locie 项目源码 文件源码
def __init__(self, simul, keys=None,
                 line_kwargs={},
                 fig_kwargs={},
                 default_fig_kwargs={"width": 600, "height": 400},
                 default_line_kwargs={},
                 notebook=True,
                 stack=False):
        from bokeh.io import push_notebook, output_notebook
        from bokeh.plotting import figure, show, ColumnDataSource
        from bokeh.layouts import Column

        if notebook:
            output_notebook()

        setattr(self, '_push', push_notebook)

        keys = keys if keys else [
            key for key in simul.fields.keys() if key != 'x']

        self._datafunc = {'x': lambda t, fields, key: fields.x}
        for key in keys:
            if isinstance(key, str):
                self._datafunc[key] = lambda t, fields, key: fields[key]
            if isinstance(key, (tuple, list)):
                self._datafunc[key[0]] = key[1]
        self._datasource = ColumnDataSource({key:
                                             func(simul.t,
                                                  simul.fields,
                                                  key)
                                             for (key, func)
                                             in self._datafunc.items()})
        self.keys = list(self._datafunc.keys())
        self.keys.remove("x")

        if stack:
            fig = figure(**default_fig_kwargs)
            for key in self.keys:
                line_config = default_line_kwargs.copy()
                line_config.update(line_kwargs.get(key, {}))
                fig.line('x', key, source=self._datasource,
                         **line_kwargs.get(key, {}))
            self._handler = show(fig, notebook_handle=True)
            return
        else:
            figs = {}
            for key in self.keys:
                fig_config = default_fig_kwargs.copy()
                fig_config.update(fig_kwargs.get(key, {}))
                line_config = default_line_kwargs.copy()
                line_config.update(line_kwargs.get(key, {}))
                figs[key] = figure(**fig_config, title=key)
                figs[key].line('x', key, source=self._datasource,
                               **line_config)

            self._handler = show(Column(*[figs[key]
                                          for key
                                          in self._datafunc.keys()
                                          if key != 'x']),
                                 notebook_handle=True)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号