在 Altair 的折线图末尾添加标签
Adding labels at end of line chart in Altair
所以我一直在努力让每行末尾都有一个标签给出国家名称,然后我可以删除图例。尝试过 transform_filter
但没有成功。
我使用了这里的数据 https://ourworldindata.org/coronavirus-source-data 我清理并重塑了数据,使其看起来像这样:-
index days date country value
0 1219 0 2020-03-26 Australia 11.0
1 1220 1 2020-03-27 Australia 13.0
2 1221 2 2020-03-28 Australia 13.0
3 1222 3 2020-03-29 Australia 14.0
4 1223 4 2020-03-30 Australia 16.0
5 1224 5 2020-03-31 Australia 19.0
6 1225 6 2020-04-01 Australia 20.0
7 1226 7 2020-04-02 Australia 21.0
8 1227 8 2020-04-03 Australia 23.0
9 1228 9 2020-04-04 Australia 30.0
import altair as alt
countries_list = ['Australia', 'China', 'France', 'Germany', 'Iran', 'Italy','Japan', 'South Korea', 'Spain', 'United Kingdom', 'United States']
chart = alt.Chart(data_core_sub).mark_line().encode(
alt.X('days:Q'),
alt.Y('value:Q', scale=alt.Scale(type='log')),
alt.Color('country:N', scale=alt.Scale(domain=countries_list,type='ordinal')),
)
labels = alt.Chart(data_core_sub).mark_text().encode(
alt.X('days:Q'),
alt.Y('value:Q', scale=alt.Scale(type='log')),
alt.Text('country'),
alt.Color('country:N', legend=None, scale=alt.Scale(domain=countries_list,type='ordinal')),
).properties(title='COVID-19 total deaths', width=600)
alt.layer(chart, labels).resolve_scale(color='independent')
这是图表当前的混乱状况。
我怎样才能只显示最后一个 'country' 名字?
编辑
这是结果。我可能会考虑单独调整某些国家/地区,因为作为一个整体进行调整意味着无论我如何处理 dx
和 dy
对齐方式,某些标签的位置总是很糟糕。
您可以通过聚合 x 和 y 编码来做到这一点。您希望文本位于最大 x 值处,因此您可以在 x 中使用 'max'
聚合。对于 y 值,您希望 y 值与最大 x 值关联,因此您可以使用 {"argmax": "x"}
聚合。
稍微调整一下文本对齐方式,结果如下所示:
labels = alt.Chart(data_core_sub).mark_text(align='left', dx=3).encode(
alt.X('days:Q', aggregate='max'),
alt.Y('value:Q', aggregate={'argmax': 'days'}, scale=alt.Scale(type='log')),
alt.Text('country'),
alt.Color('country:N', legend=None, scale=alt.Scale(domain=countries_list,type='ordinal')),
).properties(title='COVID-19 total deaths', width=600)
所以我一直在努力让每行末尾都有一个标签给出国家名称,然后我可以删除图例。尝试过 transform_filter
但没有成功。
我使用了这里的数据 https://ourworldindata.org/coronavirus-source-data 我清理并重塑了数据,使其看起来像这样:-
index days date country value
0 1219 0 2020-03-26 Australia 11.0
1 1220 1 2020-03-27 Australia 13.0
2 1221 2 2020-03-28 Australia 13.0
3 1222 3 2020-03-29 Australia 14.0
4 1223 4 2020-03-30 Australia 16.0
5 1224 5 2020-03-31 Australia 19.0
6 1225 6 2020-04-01 Australia 20.0
7 1226 7 2020-04-02 Australia 21.0
8 1227 8 2020-04-03 Australia 23.0
9 1228 9 2020-04-04 Australia 30.0
import altair as alt
countries_list = ['Australia', 'China', 'France', 'Germany', 'Iran', 'Italy','Japan', 'South Korea', 'Spain', 'United Kingdom', 'United States']
chart = alt.Chart(data_core_sub).mark_line().encode(
alt.X('days:Q'),
alt.Y('value:Q', scale=alt.Scale(type='log')),
alt.Color('country:N', scale=alt.Scale(domain=countries_list,type='ordinal')),
)
labels = alt.Chart(data_core_sub).mark_text().encode(
alt.X('days:Q'),
alt.Y('value:Q', scale=alt.Scale(type='log')),
alt.Text('country'),
alt.Color('country:N', legend=None, scale=alt.Scale(domain=countries_list,type='ordinal')),
).properties(title='COVID-19 total deaths', width=600)
alt.layer(chart, labels).resolve_scale(color='independent')
这是图表当前的混乱状况。
我怎样才能只显示最后一个 'country' 名字?
编辑
这是结果。我可能会考虑单独调整某些国家/地区,因为作为一个整体进行调整意味着无论我如何处理 dx
和 dy
对齐方式,某些标签的位置总是很糟糕。
您可以通过聚合 x 和 y 编码来做到这一点。您希望文本位于最大 x 值处,因此您可以在 x 中使用 'max'
聚合。对于 y 值,您希望 y 值与最大 x 值关联,因此您可以使用 {"argmax": "x"}
聚合。
稍微调整一下文本对齐方式,结果如下所示:
labels = alt.Chart(data_core_sub).mark_text(align='left', dx=3).encode(
alt.X('days:Q', aggregate='max'),
alt.Y('value:Q', aggregate={'argmax': 'days'}, scale=alt.Scale(type='log')),
alt.Text('country'),
alt.Color('country:N', legend=None, scale=alt.Scale(domain=countries_list,type='ordinal')),
).properties(title='COVID-19 total deaths', width=600)