首页 > 解决方案 > 为什么 jupyter-notebook 不运行交互式绘图?

问题描述

我已经看到jupyter-lab可以显示一些交互式绘图,例如plot.lyaltair,但是当我在其中绘制相同的代码时jupyter-notebook,没有任何绘图

import altair as alt
from vega_datasets import data

source = data.cars()

alt.Chart(source).mark_point().encode(
   x='Horsepower',
   y='Miles_per_Gallon',
   size='Acceleration'
)

输出只是一个VegaLite 2 对象,但没有任何绘图。

为什么我不能使用 jupyter-notebook 来绘制交互图?

标签: pythonjupyter-notebookjupyter-labaltair

解决方案


要在 Jupyter Notebook(不是 JupyterLab)中显示 altair 图,您需要安装vega python 包并通过运行alt.renderers.enable('notebook'). Altair 的安装说明中明确提到了这一点。

Altair 的文档中提供了更多故障排除信息,包括您所看到的特定输出的故障排除。


推荐阅读