Altair: Control legend values for size encoding

Created on 22 Aug 2018  路  2Comments  路  Source: altair-viz/altair

Hi Jake,

When I use the size channel for encoding the legend automatically chooses reasonable defaults for the legend. Am I allowed to control the number of steps that are chosen for the legend? For example, perhaps I would like it to go up in steps of 2, or 10, or some other value.

I would have thought that rangeStep would have controlled this but that didn't have an effect as far as I could tell.

I am gathering up all of these tips into an appendix on customization for my Altair workshop this week as I anticipate people will ask about finer controls (although I won't have much time to discuss them).

import altair as alt
from vega_datasets import data
weather=data.weather()

alt.Chart(weather).mark_point().encode(
    x='temp_max',
    y='temp_min',
    size=alt.Size('precipitation', scale=alt.Scale(domain=[0,100], range=[1,300]))
)

question

Most helpful comment

You can control that using the legend property of the encoding. For example:

import altair as alt
from vega_datasets import data

weather=data.seattle_weather()

alt.Chart(weather).mark_point().encode(
    x='temp_max',
    y='temp_min',
    size=alt.Size('precipitation',
                  scale=alt.Scale(domain=[0,100], range=[1,300]),
                  legend=alt.Legend(values=[0, 50, 100]))
)

visualization 45

All 2 comments

You can control that using the legend property of the encoding. For example:

import altair as alt
from vega_datasets import data

weather=data.seattle_weather()

alt.Chart(weather).mark_point().encode(
    x='temp_max',
    y='temp_min',
    size=alt.Size('precipitation',
                  scale=alt.Scale(domain=[0,100], range=[1,300]),
                  legend=alt.Legend(values=[0, 50, 100]))
)

visualization 45

I should have caught that! Thanks Jake!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tonylee3399 picture tonylee3399  路  3Comments

mroswell picture mroswell  路  4Comments

SuperShinyEyes picture SuperShinyEyes  路  3Comments

dzonimn picture dzonimn  路  3Comments

DentonGentry picture DentonGentry  路  3Comments