首页 > 解决方案 > 使用 VConcat 在 Streamlit 中未显示 Altair 图表

问题描述

我尝试使用 streamlit 投影 altair 图表。我的要求是以这样的方式投影两个图表,如果我在上面的图表中选择几个带有分散点的点,我应该在下面的图表中看到变量('notes')的分布。为此,我编写了以下代码,我在函数中使用了 vconcat。但是,当我使用 vconcat 时,图表永远不会出现。但是,当我尝试投影单个图表时它工作正常。

    def altair_graph(embd_1):
    
        selected = alt.selection_single(on="click", empty="none")
        brush = alt.selection(type='interval')
    
        dom = ['Other IDs', 'Slected ID','Sel Dims']
        rng_clr = ['lightgrey', 'red','blue']
    
        color_point=alt.Color('color', scale=alt.Scale(domain=dom, range=rng_clr))
    
        color = alt.condition(selected, alt.value('red'), color_point,legend=None)
    
        chart = alt.Chart(embd_1).mark_circle(size=30).encode(
            x = 'dimention1:Q', 
            y = 'dimention2:Q',
            tooltip=['ID','notes'] ,
            color=color
            ).properties(width=600,height=600).add_selection(brush).add_selection(selected).interactive()
    
        bars = alt.Chart(embd_1).mark_bar().encode(
                y='notes:N',
                color='notes:N',
                x='count(notes):Q'
                ).transform_filter(brush).interactive()
        
        #final_chart = ((chart & bars))
        final_chart = alt.vconcat(chart,bars)
    
        return final_chart

selected=altair_component(altair_chart=altair_graph(embd_1))

标签: altairstreamlit

解决方案


从您的代码片段中,我假设您正在使用streamlit-vega-lite自定义组件的 altair-part。目前似乎无法使用 streamlit-vega-lite 组件从复合图表中检索选择

也就是说,我并不完全清楚为什么图表根本没有显示。如果没有最小的可重现示例,我们将无法进行测试。我最近遇到了一个类似的案例,它可以单独绘制图表,也可以将图表组合在一起。选择也可以这样工作,但是,值不会反映在从altair_component


推荐阅读