Search code examples
pythonaltairvega-lite

How to reproduce the Unsub histogram in Altair?


Unsub's histogram display stacks each journal title on top of each other as an individual box, then ranks by cost per use on the x-axis.

https://i.ibb.co/2FvXhFp/unsub.jpg (can't post images due to my new account)

I want to reproduce this in Altair, I can't figure out how to "break up" the histogram's bars.

https://i.ibb.co/jzX4N2r/altair.jpg

    hist = alt.Chart(df[filt_to_100]).mark_bar().encode(
    alt.X('cpu:Q', bin=alt.Bin(maxbins=100)),
    y='count()'
    ).interactive()
hist

I'm sure it has something to do with the y=count() function, but I can't find a way to make it show individual points. I also tried switching it to a mark_circle(), but that doesn't look right either.


Solution

  • You can replicate it to an extent via the detail parameter. However, the white lines are not added for each observations, I'm not sure if it is because they are aligning to play nicely with what is on the axis (I tried setting nice=False but to no avail), maybe someone more knowledgeable can fill in.

    import altair as alt
    from vega_datasets import data
    
    source = data.cars()
    
    alt.Chart(source.reset_index(), height=200).mark_bar().encode(
        alt.X("Horsepower", bin=alt.Bin(maxbins=50)),
        alt.Y('count()', axis=alt.Axis(grid=False)),
        alt.Detail('index')
    ).configure_view(strokeWidth=0)
    

    enter image description here