简体   繁体   中英

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. 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. How can I make the y-axis selection responsive? 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 .

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).

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
)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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