在带有标记文本的 altair 中对条形图进行排序的问题

kan*_*256 2 python altair

chart_df= alt.Chart(df).mark_bar().encode(
    x = 'value',
    y = alt.Y('name', sort='-x'),
    color = 'variable'
)
Run Code Online (Sandbox Code Playgroud)

为了将每个条的值添加为文本,我使用了波纹管代码,但我丢失了排序的条。

chart_df_text = chart_df.mark_text().encode(
    x = 'text_margin_from_bar:Q',
    text = 'human_readable_value:Q',
).transform_calculate(
    human_readable_value = expr.toString(expr.floor(datum.value/10**7)),
    text_margin_from_bar = datum.value + (datum.value/expr.abs(datum.value))*1000000000
    # i have negetive and positive numbers, so for have a space between number and bar, i do this
)
Run Code Online (Sandbox Code Playgroud)

添加

y = alt.Y('name', sort='-x'),
Run Code Online (Sandbox Code Playgroud)

到 chart_df_text 但我仍然有问题。我读了另一个有我问题的问题,说问题是 Altair 的版本,但我在最后一个。

jak*_*vdp 5

javascript 控制台中的警告告诉您为什么这不起作用:

[Warning] Dropping sort property {"field":"value","op":"sum"} as unioned domains only support boolean or op "count", "min", and "max".
[Warning] Domains that should be unioned has conflicting sort properties. Sort will be set to true.
Run Code Online (Sandbox Code Playgroud)

您可以通过op在您的排序中使用支持来解决此问题。例如:

[Warning] Dropping sort property {"field":"value","op":"sum"} as unioned domains only support boolean or op "count", "min", and "max".
[Warning] Domains that should be unioned has conflicting sort properties. Sort will be set to true.
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明