我发现holoviz面板是构建数据可视化仪表板的一个非常有趣的解决方案。不幸的是,我在让节点链接图的 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(。
任何帮助使它工作非常感谢...
谢谢 一月。
截图1- :截图1 截图 2
- :截图 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"
所以结论是这是面板中的一个错误,并由@philippjfr迅速修复: https://github.com/holoviz/panel/issues/872
要完成这项工作,您需要面板 0.7.1。
由于此版本尚不可用,因此您可以使用以下方法安装最新版本的面板:
pip install git+https://github.com/holoviz/panel.git
Vega 图可以用最新版本的面板显示,如下所示:
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 上的讨论