简体   繁体   English

使下拉选择响应 y 轴 Altair python

[英]Make dropdown selection responsive for y axis Altair python

I have several columns (eg, Column, y1, y2, y3..) that I need to relate to column "X" on a scatter plot in Altair.我有几列(例如,Column、y1、y2、y3..)需要与 Altair 中散点图 plot 上的列“X”相关联。 I have included a dropdown combo box to make the selection between the "y" columns however the plots fail to change according to the selection.我已经包含一个下拉组合框来在“y”列之间进行选择,但是绘图无法根据选择进行更改。 How can I make the y-axis selection responsive?如何使 y 轴选择响应? Here is the code这是代码

# CHART 1
input_dropdown =  alt.binding_select(options = \
                                     np.array(df.drop(["Student IDs", "Average Marks"], 
                                             axis = 1).columns),
                                    name = "Module")

selection = alt.selection_single(bind = input_dropdown)


# plot the first chart
chart1 = alt.Chart(df).mark_point().encode(
    x = "Average Marks",
    y = "CSE103"
).add_selection(
    selection)

chart1

散点图在这里

This is not directly supported in Vega-Lite, you can add your thumbs up and subscribe to this issue to find out when/if it is implemented https://github.com/vega/vega-lite/issues/7365 . Vega-Lite 不直接支持此功能,您可以竖起大拇指并订阅此问题以了解何时/是否实施https://github.com/vega/vega-lite/issues/7365

In the meantime, you could workaround it using the same approach as in Altair heatmap with dropdown variable selector , where the data frame is first melted (but you can't dynamically change the axis title).同时,您可以使用与Altair heatmap 相同的方法和下拉变量 selector来解决它,其中数据框首先被融化(但您不能动态更改轴标题)。

import altair as alt
from vega_datasets import data


df = data.cars().melt(id_vars=['Origin', 'Name', 'Year'])
dropdown = alt.binding_select(options=list(df.variable.drop_duplicates()))
selection = alt.selection_single(fields=['variable'], bind=dropdown, name='X_axis')

alt.Chart(df).mark_circle(size=60).encode(
    x=alt.X('value:Q', title=''),
    y='Origin',
    color='Origin',
).add_selection(
    selection
).transform_filter(
    selection
)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM