gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:07:13 30 4
gpt4 key购买 nike

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

为了将每个条形图的值添加为文本,我使用了以下代码,但我丢失了排序的条形图。

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
)

添加

y = alt.Y('name', sort='-x'),

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

最佳答案

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.

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

import pandas as pd
import altair as alt
from altair import expr, datum

df = pd.DataFrame({
'value': [-3E9, -4E9, 6E9, 1E10, -8E9],
'name': ['Bob', 'Sue', 'Tim', 'Ann', 'Bill'],
'variable': range(5)
})

chart_df= alt.Chart(df).mark_bar().encode(
x = 'value',
y = alt.Y('name', sort=alt.EncodingSortField('value', op='min', order='descending')),
color = 'variable'
)

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
)

chart_df + chart_df_text

enter image description here

关于python - 在带有标记文本的 altair 中对条形图进行排序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62026555/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com