3

我想使用 Python 2.7.12 和 Altair 1.2.0 可视化网页上的一些图表。

使用他们的例子很容易和直接:

from altair import *
from altair import Chart, load_dataset

# load built-in dataset as a pandas DataFrame
cars = load_dataset('cars')

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

chart.display()  

这适用于 Jupyter Notebook。

更改chart.display()chart.server()(如https://altair-viz.github.io/documentation/displaying.html#displaying-plots-via-a-local-http-server所述)应该足以使用 Python 的 HTTPServer 启动 Web 服务器。

它启动,但它提供的页面是空的。源在那里,但没有可视化。

知道为什么吗?

4

1 回答 1

0

添加我之前的评论作为答案:

chart.serve() 按预期工作(altair 2.1.0)。文档已移至此处https://altair-viz.github.io/user_guide/display_frontends.html?e#working-in-non-notebook-environments

from altair import Chart, load_dataset

# load built-in dataset as a pandas DataFrame
cars = load_dataset('cars')

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

chart.serve() 
于 2021-01-29T02:55:24.833 回答