简体   繁体   English

在 Altair 的折线图末尾添加标签

[英]Adding labels at end of line chart in Altair

So I have been trying to get it so there is a label at the end of each line giving the name of the country, then I can remove the legend.所以我一直在努力让它在每一行的末尾都有一个 label 给出国家的名称,然后我可以删除图例。 Have tried playing with transform_filter but no luck.试过玩transform_filter但没有运气。

I used data from here https://ourworldindata.org/coronavirus-source-data I cleaned and reshaped the data so it looks like this:-我使用了这里的数据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')

This is the current mess that the chart is in.这是图表当前的混乱情况。

在此处输入图像描述

How would I go about just showing the last 'country' name?我如何 go 只显示最后一个“国家”名称?

EDIT编辑

Here is the result.这是结果。 I might look at adjusting some of the countries separately as adjusting as a group means that some of the labels are always badly positioned no matter what I do with the dx and dy alignment.我可能会考虑单独调整一些国家/地区,因为作为一个整体进行调整意味着无论我用dxdy alignment 做什么,一些标签总是位置不佳。

在此处输入图像描述

You can do this by aggregating the x and y encodings.您可以通过聚合 x 和 y 编码来做到这一点。 You want the text to be at the maximum x value, so you can use a 'max' aggregate in x.您希望文本处于最大 x 值,因此您可以在 x 中使用'max'聚合。 For the y-value, you want the y value associated with the max x-value, so you can use an {"argmax": "x"} aggregate.对于 y 值,您希望 y 值与最大 x 值相关联,因此您可以使用{"argmax": "x"}聚合。

With a bit of adjustment of text alignment, the result looks like this:对文本 alignment 稍作调整,结果如下所示:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM