Can Sucuoglu
Can Sucuoglu

Reputation: 195

How to make aggregated point sizes bigger in Altair, Python?

I am working on a dashboard using Altair. I am creating 4 different plot using the same data. I am creating scatterplots using mark_circle.

How do I change the size to be size*2, or anything else?

Here is a sample:

bar = alt.Chart(df).mark_point(filled=True).encode(
    x='AGE_GROUP:N',
    y=alt.Y( 'PERP:N', axis=alt.Axis( values= df['PERP'].unique().tolist() )),
    size = 'count()')

Upvotes: 4

Views: 5315

Answers (1)

jakevdp
jakevdp

Reputation: 86463

You can do this by adjusting the scale range for the size encoding. For example, this sets the range such that the smallest points have an area of 100 square pixels, and the largest have an area of 500 square pixels:

import altair as alt
import pandas as pd
import numpy as np

df = pd.DataFrame({
    'x': np.random.randn(30),
    'y': np.random.randn(30),
    'count': np.random.randint(1, 5, 30)
})

alt.Chart(df).mark_point().encode(
    x='x',
    y='y',
    size=alt.Size('count', scale=alt.Scale(range=[100, 500]))
)

enter image description here

Upvotes: 8

Related Questions