如何动态添加/修改/修补 altair 图表数据?

How to add/ modify/ patch altair chart data dynamically?

我想向现有图表动态添加图表或数据(通过 ipywidget.interact),如以下代码 (chart + dotchart) 所示。我几乎得到了我想要的,除了重新绘制了整个图表,这会导致闪烁。

如何动态添加/修改/修补数据并避免重新绘制整个图表?

谢谢!

import pandas as pd
import numpy as np
import altair as alt
from ipywidgets import interact

df = pd.DataFrame({"xval": range(100), "yval": np.random.randint(0,100,100)})
chart = alt.Chart(df).mark_point().encode(x="xval", y="yval",)

def update(x, y):
    dot = pd.DataFrame(dict(x=[x], y=[y]))
    dotchart = alt.Chart(dot).mark_point().encode(x="x", y="y", color=alt.value("red"))
    return chart + dotchart 

interact(update, x=(0, 100), y=(0, 100))
# x, y widgets that control position of 'red dot'

将数据修补到 Altair 图表而不重新渲染的唯一方法是在 Javascript 中,使用 Vega View API. You can see an example of this here: https://vega.github.io/vega-lite/tutorials/streaming.html.

我不知道从 Python 调用 Vega 视图 API 的任何先前工作,但原则上是可能的。

在此处查看相关的 Altair 功能请求:https://github.com/altair-viz/altair/issues/426