【问题标题】:Altair not rendering chart in jupyter notebookAltair 不在 jupyter 笔记本中渲染图表
【发布时间】:2018-10-27 07:09:35
【问题描述】:

我想使用 altair 在 Jupyter notebook 中查看图表。所以我尝试了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

我遇到了一个错误:

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

我正在使用 Jupyter 实验室。使用 Ipython 版本 > 5。但我仍然只得到文本输出。

【问题讨论】:

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


【解决方案1】:

当您在 Jupyter 笔记本中看到如上所示的文本输出时,这意味着您使用的 IPython 内核尚不支持基于 mimetype 的输出呈现。

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

import IPython
IPython.__version__
# 6.2.1

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

在 JuptyerLab 中,动态输出(如 Altair 绘图)的渲染更加清晰:如果可能,我建议改用它。

【讨论】:

  • 嗨,杰克。喜欢 youtube 上的视频。我正在使用 Ipython 版本>5 的 Jupyterlab。已在问题部分添加了屏幕截图。
  • 尝试升级到 IPython 版本 6... 我不确定添加 mimetype 渲染的确切时间,但您看到的行为是由于 ipython 版本较旧。您可能也遇到与此人相同的问题:github.com/altair-viz/altair/issues/809
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
  • 1970-01-01
  • 2015-10-15
  • 2021-10-11
  • 1970-01-01
  • 2019-03-17
  • 2022-07-21
相关资源
最近更新 更多