如何动态添加/修改/修补牵牛星图表数据?

2024-04-26 07:41:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想动态地(通过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'

Tags: 数据importdataframedfasnpchart图表
1条回答
网友
1楼 · 发布于 2024-04-26 07:41:09

将数据修补到Altair图表中而不重新呈现的唯一方法是使用Javascript,使用Vega View API。你可以在这里看到一个例子:https://vega.github.io/vega-lite/tutorials/streaming.html

我不知道之前有什么从Python调用Vega视图API的工作,但原则上这是可能的

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

相关问题 更多 >