Altair不在jupyter笔记本中渲染图表

时间:2018-05-17 05:58:15

标签: python python-2.7 jupyter-notebook vega-lite altair

我想使用altair查看Jupyter笔记本中的图表。所以我尝试了tutorial examples。我已经安装了所需的包。 这是我的代码:

import altair as alt
from vega_datasets import data

# Uncomment/run this line to enable Altair in the classic notebook
#   (this is not necessary in JupyterLab)
alt.renderers.enable('notebook')

cars = data.cars()

chart = alt.Chart(cars).mark_circle().encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
)

chart

我得到的是字典而不是图表。像这样:

Chart({
  data:      Acceleration  Cylinders  Displacement  Horsepower  Miles_per_Gallon  \
  0            12.0          8         307.0       130.0              18.0   
  1            11.5          8         350.0       165.0              15.0   
  2            11.0          8         318.0       150.0              18.0   
  3            12.0          8         304.0       150.0              16.0

使用此命令安装数据集时:

conda install -c conda-forge altair vega_datasets notebook vega3

我遇到了一个错误: enter image description here

这个错误可能是原因还是有其他问题?

我正在使用Jupyter实验室。使用Ipython版本>但我仍然只得到文字输出。 enter image description here

1 个答案:

答案 0 :(得分:0)

当您看到上面在Jupyter笔记本中看到的文本输出时,这意味着您正在使用的IPython内核尚不支持基于mimetype的输出渲染。

要解决此问题,您需要将IPython更新为最新版本。要检查IPython的版本,请在笔记本中运行

import IPython
IPython.__version__
# 6.2.1

您最好使用IPython 5或更高版本。

在JuptyerLab中渲染像Altair图这样的动态输出更清晰:我建议切换到使用它,如果可能的话。