【问题标题】:Vega plot not displaying within a holoviz panel in jupyter notebookVega 图未显示在 jupyter notebook 的 holoviz 面板中
【发布时间】:2019-12-15 11:18:31
【问题描述】:

我发现holoviz panel 是构建数据可视化仪表板的一个非常有趣的解决方案。不幸的是,我在获取节点链接图的 vega 图以在 jupyter 笔记本的面板中工作时遇到一些问题。

相关进口等:

  • import panel
  • pn.extension()
  • from vega import Vega

我的发现:

  • vega 导入在面板外使用时效果很好:从https://vega.github.io/editor/#/examples/vega/force-directed-layout 复制/粘贴的 Vega 规范可视化,因为它应该使用 Vega(spec)(参见屏幕截图 1)。
  • 使用pn.pane.Vega(spec) 时,我得到一个空白空间。使用pn.pane.Vega(spec).show() 在外部运行可视化并查看源代码,我发现 div 是空的(参见屏幕截图 2)。

非常感谢您对这项工作的任何帮助...

谢谢你, 一月。

这是显示问题的最小脚本:

#!/usr/bin/env python
import panel as pn
from bokeh.plotting import output_notebook
from vega import Vega
pn.extension('vega')
output_notebook()

spec = {
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "width": 400,
  "height": 200,

  "data": [
    {
      "name": "table",
      "values": [
        {"category": "A", "amount": 28},
        {"category": "B", "amount": 55},
        {"category": "C", "amount": 43}
      ]
    }
  ],

  "scales": [
    {
      "name": "xscale",
      "type": "band",
      "domain": {"data": "table", "field": "category"},
      "range": "width"
    },
    {
      "name": "yscale",
      "domain": {"data": "table", "field": "amount"},
      "range": "height"
    }
  ],

  "marks": [
    {
      "type": "rect",
      "from": {"data":"table"},
      "encode": {
        "enter": {
          "x": {"scale": "xscale", "field": "category"},
          "width": {"scale": "xscale", "band": 1},
          "y": {"scale": "yscale", "field": "amount"},
          "y2": {"scale": "yscale", "value": 0}
        },
        "update": {
          "fill": {"value": "steelblue"}
        }
      }
    }
  ]
}

Vega(spec) # => shows barchart => OK

pn.Column(pn.panel("## Vega test"),
          pn.pane.Vega(spec),
          pn.panel("_end of test_"))
# => shows "Vega test", then empty space, the "end of test"

pn.Column(pn.panel("## Vega test"),
          pn.panel(spec),
          pn.panel("_end of test_"))
# => shows "Vega test", then empty space, the "end of test"

【问题讨论】:

  • 你试过 pn.extension('vega') 而不是 pn.extension() 吗?
  • 是的。也这样做了,但也没有用...
  • 也许 discourse.holoviz.org 上的某个人可以帮助您?但是,如果您可以添加一个最小的代码示例来演示您的确切问题,这也会有所帮助。
  • @SandervandenOord 我在描述中添加了一个最小的脚本。
  • 我发现这个例子对我有用:panel.pyviz.org/gallery/links/vega_heatmap_link.html也许你可以根据你的例子调整它?

标签: python jupyter-notebook vega holoviz panel-pyviz


【解决方案1】:

所以结论是这是面板中的一个错误,并由@philippjfr 迅速修复: https://github.com/holoviz/panel/issues/872

要完成这项工作,您需要面板 0.7.1。

由于此版本尚不可用,您可以通过以下方式安装最新版本的面板:

pip install git+https://github.com/holoviz/panel.git

vega plot可以用最新版本的panel显示如下:

pn.Column(pn.pane.Vega(spec))

另请参阅 discourse.holoviz.org 上的讨论:
https://discourse.holoviz.org/t/vega-plot-not-displaying-within-a-holoviz-panel-in-jupyter-notebook/49/5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2020-12-12
    • 2017-01-28
    相关资源
    最近更新 更多