我想通过下面的代码(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'
答案 0 :(得分:1)
将数据修补到Altair图表中而不重新呈现数据的唯一方法是使用Vega View API使用Javascript。您可以在这里看到一个示例:https://vega.github.io/vega-lite/tutorials/streaming.html。
我之前不知道从Python调用Vega视图API的任何工作,但是原则上是可行的。
在此处查看相关的Altair功能请求:https://github.com/altair-viz/altair/issues/426。