简体   繁体   中英

Altair: how to add a fixed set of labels for an axis

I've got a dataset that looks like so:

    position    reputation
0   RW          5
1   ST          5
2   LW          5
3   GK          3
4   LW          4

What I've got now is a way of seeing how many players from each position have a particular reputation, letting the user select the minimum reputation with a slider:

slider = pn.widgets.IntSlider(name='Integer Slider', start=1, end=5, step=1, value=3)

@pn.depends(slider.param.value)
def get_plot(min_rep):
     min_reputation = slider.value
     # create filter mask for the dataframe
     mask = (pbp['reputation'] >= min_reputation)
     tmp = pbp.loc[mask] # count of position
     # TODO: make this percent of each position, rather than count
     chart = alt.Chart(tmp).mark_bar().encode(
         x=alt.X('position'), 
         y='count(position)', 
         color=alt.Color('reputation:N')
     )
     return chart

The problem with this is that when the user selects a particular reputation, that also gets rid of any records that don't have a 'reputation' above the limit and thus we have different numbers of "positions" that we pull out of the dataset. This is kind of visually jarring since the chart completely resizes and I've spent a long time trying to figure out how to have a fixed array of values for X and then count how many records are in each of those categories for Y. I've dug into transforms and filters but haven't had any luck yet. Altair/Vega is very data-driven so I may have to restructure my data into something that's like:

    position    reputation_5   reputation_4   reputation_3   reputation_2   reputation_1
0   RW          1              10             100            200            1000

I'd imagine there's a way to tell the Y axis to count each of the selected categories, but it'd be neat to find a way to do this in a more clever way. Thanks for any pointers!

Try setting a constant width with .properties(width=200)

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