如何在 Altair 中重现 Unsub 直方图?

How to reproduce the Unsub histogram in Altair?

Unsub 的直方图显示将每个期刊名称堆叠在一起作为一个单独的框,然后在 x-axis 上按每次使用成本排名。

https://i.ibb.co/2FvXhFp/unsub.jpg(由于我的新帐户不能 post 图片)

我想在 Altair 中重现这个,我不知道如何“分解”直方图的条形。

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

我确定它与 y=count() 函数有关,但我找不到让它显示单个点的方法。我也尝试将其切换为 mark_circle(),但这看起来也不正确。

您可以通过 detail 参数在一定程度上复制它。但是,没有为每个观察结果添加白线,我不确定是不是因为它们对齐以与轴上的内容很好地配合(我尝试设置 nice=False 但无济于事),也许有人有知识的可以填写。

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)

另一种具有类似结果的方法是重新格式化您的数据框,使其具有从 0 到每个 bin 的最大计数的列,然后使用 mark_rect:

绘制它
import altair as alt
from vega_datasets import data

source = data.cars()

# Add index from zero to max count (bin height) for each group
source2 = source.groupby('Horsepower', as_index=False).apply(lambda x: x.reset_index(drop = True)).reset_index()

alt.Chart(source2, height=200, width=800).mark_rect(size=8, strokeWidth=1, stroke='white').encode(
    alt.X('Horsepower:O'),
    alt.Y('level_1:O', title='Count', scale=alt.Scale(reverse=True)),
)

如果您希望它更类似于直方图 x 轴,您可以使用 pd.cut 将马力分成区间,但您不能直接在 Altair 中使用区间数据类型,因此您需要为每个垃圾桶分配一个编号。