如何在选择过程中动态更改 Altair 中的轴标签

问题描述 投票:0回答:1

无法动态更改 Altair 中连通图中的轴值

cor_data = alt.UrlData(url="https://raw.githubusercontent.com/keto08/covid-19/master/COVID19-UK/cor_data.csv")
df2 = alt.UrlData(url="https://raw.githubusercontent.com/keto08/covid-19/master/COVID19-UK/heatmap_scatter.csv")

var_sel_cor = alt.selection_single(fields=['variable', 'variable2'], clear=False, 
                                  init={'variable': 'value1', 'variable2': 'value2'})
base = alt.Chart(cor_data,title="Corellation Among Inputs and Outputs").encode(
    x=alt.X('variable2:O',title=""),
    y=alt.Y('variable:O',title="")    
)
text = base.mark_text().encode(
    text='correlation_label:O',
    color=alt.condition(
        alt.datum.correlation > 0.5, 
        alt.value('white'),
        alt.value('black')
    )
)
cor_plot = base.mark_rect().encode(alt.Color('correlation:Q',
                  scale=alt.Scale(
                            scheme='redblue',
                            domain=[-1, 0, 1],
                            type='linear'))).add_selection(var_sel_cor)
zoom = alt.selection_interval(
    bind='scales',
    on="[mousedown[!event.shiftKey], mouseup] > mousemove",
    translate="[mousedown[!event.shiftKey], mouseup] > mousemove!",
)

selection = alt.selection_interval(
    on="[mousedown[event.shiftKey], mouseup] > mousemove",
    translate="[mousedown[event.shiftKey], mouseup] > mousemove!",
)

scat_plot = alt.Chart(df2).transform_filter(
    var_sel_cor
).mark_circle(opacity=0.5).encode(
    x=alt.X('value1:Q'), 
    y=alt.Y('value2:Q'),
    size=alt.Size('population:Q',legend=alt.Legend(padding=35,offset=5)),
).add_selection(zoom,selection)

alt.data_transformers.enable('default', max_rows=None)

alt.hconcat((cor_plot + text).properties(width=500, height=500), scat_plot.properties(width=350, height=350)).resolve_scale(color='independent')

这段代码给了我Heatmap with Connected Scatter plot

我想动态更改连接散点图的轴标签(x 轴和 y 轴)。 任何人都可以建议我如何做到这一点。

python scatter-plot altair vega-lite f#-interactive
1个回答
1
投票

这在 Altair 中尚不可能实现。解决方法可能是使用

mark_text
创建一个带有正确标签的单独图表(如 我可以将 Altair 轴标题转换为链接吗?)并缩小间距。未来它可能在 Vega-Lite 中成为可能,并被建议作为一项功能 https://github.com/vega/vega-lite/issues/7264。我认为你的例子是一个很好的用例,当这个功能有用时。

相关问题

© www.soinside.com 2019 - 2024. All rights reserved.