最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - For an altair line chart, can you order values in a tooltip by values of a second variable? - Stack Overflow

programmeradmin6浏览0评论

I have created a line chart in Altair to visualise percentages (VALUE) by month (DATE) for a number of countries (COUNTRY). This chart also has two dropdown menus, which control the measure shown (MEASURE) and the topic (TOPIC).

I wanted the lines to be layered in a specific order on the chart, so used alt.Color to rank the order of the lines:

df[COUNTRY] = pd.Categorical(df[COUNTRY], categories=COUNTRY_ORDER, ordered=True)
chart = alt.Chart(df).mark_line() ... ... color = alt.Color(COUNTRY, scale=alt.Scale(domain=COUNTRY, range=colour_palette), sort=COUNTRY_ORDER) ... .add_params(selection_measure) .transform_filter(selection_measure)
chart = chart.add_params(selection_topic).transform_filter(selection_topic)

I then have a tooltip to show the percentage (VALUE) for each country (COUNTRY) in each month (DATE):

rules = alt.Chart(df).transform_filter(selection_measure) .transform_filter(selection_topic).transform_pivot(COUNTRY, value=VALUE, groupby=[DATE]) ... .encode(x=DATE, ... tooltip=[alt.Tooltip(DATE, ...)] + [alt.Tooltip(c, type="quantitative", format=".0%") for c in df[COUNTRY]], ... .add_params(selection_measure, selection_topic)

The order of COUNTRY in my tooltip is the same as the order of COUNTRY that I used for alt.Color, in order to control line order.

I find this difficult to read values off of the chart like this, and would like to make the order of COUNTRY in the tooltip instead be ranked based on VALUE, in descending order.

Ideally, I also want to rank COUNTRY in the tooltip based on VALUE with the specific selection chosen in the dropdown menus.

Is this possible?

Thanks in advance

I couldn't find anything on here or on Altair examples about this issue, but I may have missed something that would help here!

I have considered creating a separate chart for one month only which may be easier to rank, and could go alongside this chart for ease, but haven't tried this yet.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论