运行 来自终端的脚本时 Altair 不显示图表?

Altair doesn't display charts when running a script from terminal?

我正在我的 Mac Pro Big Sur 上学习这个教程示例。

https://altair-viz.github.io/gallery/simple_bar_chart.html

vtest.py如下:

import altair as alt
import pandas as pd

source = pd.DataFrame({
    'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
    'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})

alt.Chart(source).mark_bar().encode(
    x='a',
    y='b'
)

当我在终端上执行此操作时,它没有显示任何内容,没有可视化显示,也没有错误或警告消息。

% python vtest.py

Altair 不能在 Mac OS 上工作吗?

如果您想通过 运行 终端脚本显示 Altair 绘图,您可以使用 .show() 方法在默认浏览器中打开它:

alt.Chart(source).mark_bar().encode(
    x='a',
    y='b'
).show()

文档包含 more details about how Altair charts are rendered and how to display them conveniently 部分,从这段文字开始:

Altair produces Vega-Lite visualizations, which require a Javascript frontend to display the charts. Because notebook environments combine a Python backend with a Javascript frontend, many users find them convenient for using Altair.