Getting Altair to work with Jupyter Notebook

2020-07-14 03:47发布

问题:

Trying to get Altair to work with Jupyter Notebook, I installed it using

conda install -c conda-forge altair vega_datasets notebook vega

But when I try to do an example plot

import altair as alt
from vega_datasets import data

# for the notebook only (not for JupyterLab) run this command once per session
alt.renderers.enable('notebook')

iris = data.iris()

alt.Chart(iris).mark_point().encode(
    x='petalLength',
    y='petalWidth',
    color='species'
)

as seen in their quick start guide, I get

ValueError: 
To use the 'notebook' renderer, you must install the vega package
and the associated Jupyter extension.
See https://altair-viz.github.io/getting_started/installation.html
for more information.

even though I have installed vega using Conda. I can make vega example plots though. I am unable to enable the Jupyter extension though, as Jupyter says it is incompatible.

Any help is appreciated.

回答1:

For the current release of altair (Version 2.2), use

conda install -c conda-forge vega=1.3

or

pip install vega==1.3

and then restart the notebook.

The vega python extension was mistakenly updated this week to only support vega-lite 3.0, which is not yet released and thus not yet supported by Altair.

See https://github.com/altair-viz/altair/issues/1114 for the initial bug report.