首页 > 解决方案 > How can I match the scale for 2 different variables in mark_circle() in Altair?

问题描述

I'm working with Altair and I'm having trouble overlapping 2 mark_circle() charts due to the size of my variables. I want to overlay 2 circles so I can compare the size of one variable to the size of the other variable.

The problem is that one variable is in the order of millions and the second variable is from 0 to 100 and when overlapping them the second variable is not shown due to the size compared to millions.

I tried playing with scale=alt.Scale(range=(xx,xx)) but I can't make it work, so not sure if this is possible or where am I getting lost?

Here is my code

pop = alt.Chart(df).mark_circle(color='blue').encode(
    y = alt.Y('Top_City', sort=alt.SortField(field="Rank_mean", order='ascending')),
    size = alt.Size('Pop_mean')
)
pop
fa = alt.Chart(df).mark_circle(color='yellow').encode(
    y = alt.Y('Top_City', sort=alt.SortField(field="Rank_mean", order='ascending')),
    size = alt.Size('FARS_PerCapita')
)
fa

Ideally I would like to have something like the last image when doing

pop + fa

pop

fa

Desired Outcome

标签: pythonchartsdata-visualizationaltair

解决方案


After staring at it for an extended period of time this solved my problem.

alt.layer(pop, fa).resolve_scale(size='independent')


推荐阅读