argmax 聚合和 select n altair 中的最大值
argmax aggregation and select n biggest values in altair
我正在研究这个简单的例子,基于“年”被解析为日期的数据(我不确定这是问题 #1 的根源)
alt.Chart(data).mark_bar(size=10).encode(
# alt.X("population", aggregate={"argmax": "year"}),
alt.X("max(population)"),
alt.Y("country", sort="-x"),
alt.Color("continent"),
).transform_filter("datum.population > 1e8").properties(width=400, height=200)
输出差不多就可以了。我想要的是:
- 使用
argmax
聚合来获取最新的人口值而不是最大的。如果我在评论中使用该行,则该图为空
- 用选择的 k(=10?)个人口最多的国家替换
transform_filter
。
- 如何在工具提示中也使用适当的小数点分隔符?我们可以在 x 轴上使用“1 亿”而不是“100,000,000”吗?
在编码中使用带有 argmax 聚合的条形标记时,似乎存在一个 vega-lite 错误。您可以使用 aggregate transform.
解决此问题
您可以使用 window transform to compute the rank, and then filter on this, as demonstrated in the Top K Items 示例。
可以使用 d3-format 包中记录的说明符指定轴标签格式。
将所有这些放在一起并使用一些虚假数据进行演示,这是您可能使用的方法示例:
import altair as alt
import pandas as pd
import numpy as np
data = pd.DataFrame({
'continent': 6 * ['Europe'] + 6 * ['Asia'],
'country': 3 * ['Germany'] + 3 * ['Spain'] + 3 * ['China'] + 3 * ['Japan'],
'year': 4 * [2018, 2019, 2020],
'population': np.random.randint(1E7, 1.2E8, 12),
})
alt.Chart(data).mark_bar(size=10).transform_aggregate(
most_recent_year='argmax(population)',
groupby=['country', 'continent']
).transform_calculate(
population='datum.most_recent_year.population'
).transform_window(
rank='rank(population)',
sort=[alt.SortField('population', order='descending')]
).transform_filter(
alt.datum.rank <= 3
).encode(
alt.X("population:Q", axis=alt.Axis(format='~s')),
alt.Y("country:N", sort="-x"),
alt.Color("continent:N"),
)
我正在研究这个简单的例子,基于“年”被解析为日期的数据(我不确定这是问题 #1 的根源)
alt.Chart(data).mark_bar(size=10).encode(
# alt.X("population", aggregate={"argmax": "year"}),
alt.X("max(population)"),
alt.Y("country", sort="-x"),
alt.Color("continent"),
).transform_filter("datum.population > 1e8").properties(width=400, height=200)
输出差不多就可以了。我想要的是:
- 使用
argmax
聚合来获取最新的人口值而不是最大的。如果我在评论中使用该行,则该图为空 - 用选择的 k(=10?)个人口最多的国家替换
transform_filter
。 - 如何在工具提示中也使用适当的小数点分隔符?我们可以在 x 轴上使用“1 亿”而不是“100,000,000”吗?
在编码中使用带有 argmax 聚合的条形标记时,似乎存在一个 vega-lite 错误。您可以使用 aggregate transform.
解决此问题您可以使用 window transform to compute the rank, and then filter on this, as demonstrated in the Top K Items 示例。
可以使用 d3-format 包中记录的说明符指定轴标签格式。
将所有这些放在一起并使用一些虚假数据进行演示,这是您可能使用的方法示例:
import altair as alt
import pandas as pd
import numpy as np
data = pd.DataFrame({
'continent': 6 * ['Europe'] + 6 * ['Asia'],
'country': 3 * ['Germany'] + 3 * ['Spain'] + 3 * ['China'] + 3 * ['Japan'],
'year': 4 * [2018, 2019, 2020],
'population': np.random.randint(1E7, 1.2E8, 12),
})
alt.Chart(data).mark_bar(size=10).transform_aggregate(
most_recent_year='argmax(population)',
groupby=['country', 'continent']
).transform_calculate(
population='datum.most_recent_year.population'
).transform_window(
rank='rank(population)',
sort=[alt.SortField('population', order='descending')]
).transform_filter(
alt.datum.rank <= 3
).encode(
alt.X("population:Q", axis=alt.Axis(format='~s')),
alt.Y("country:N", sort="-x"),
alt.Color("continent:N"),
)