Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I'm loving Altair for creating choropleth maps! My biggest problem, however, is I cannot figure out how to change the size of the legend. I've read through the documentation and tried several things to no avail.

Here's an example using the unemployment map by county from Altair's docs. I added a 'config' layer to change the font size for the title on both the map and the legend. Note the .configure_legend() part of the code within "config".

counties = alt.topo_feature(data.us_10m.url, 'counties')

source = data.unemployment.url

foreground = alt.Chart(counties).mark_geoshape(

    ).encode(

    color=alt.Color('rate:Q', sort="descending",  scale=alt.Scale(scheme='plasma'), legend=alt.Legend(title="Unemp Rate", tickCount=6))

).transform_lookup(

    lookup='id',

    from_=alt.LookupData(source, 'id', ['rate'])

).project(

    type='albersUsa'

).properties(

    title="Unemployment Rate by County",

    width=500,

    height=300

)

config = alt.layer(foreground).configure_title(fontSize=20, anchor="middle").configure_legend(titleColor='black', titleFontSize=14) 

config

Here's what the image should look like:

enter image description here

If I change the size of the map like this:

counties = alt.topo_feature(data.us_10m.url, 'counties')

source = data.unemployment.url

foreground = alt.Chart(counties).mark_geoshape(

    ).encode(

    color=alt.Color('rate:Q', sort="descending",  scale=alt.Scale(scheme='plasma'), legend=alt.Legend(title="Unemp Rate", tickCount=6))

).transform_lookup(

    lookup='id',

    from_=alt.LookupData(source, 'id', ['rate'])

).project(

    type='albersUsa'

).properties(

    title="Unemployment Rate by County",

    width=900,

    height=540

)

config = alt.layer(foreground).configure_title(fontSize=20, anchor="middle").configure_legend(titleColor='black', titleFontSize=14) 

config

The legend stays the same size, so that it now looks tiny in comparison to the map:

enter image description here

Alternatively, if I make the map size tiny, the legend will be huge!

enter image description here

I've tried about a dozen different things to no avail.

Anyone have a solution to this?

1 Answer

0 votes
by (41.4k points)

If you want  to adjust the size, use the configure_legend() chart method.Also, the relevant arguments for adjusting the size of the legend gradient is mentioned in the code below:

chart.configure_legend( gradientLength=400, gradientThickness=30 )

If you wish to learn more about how to use python for data science, then go through data science python programming course by Intellipaat for more insights.

Related questions

0 votes
1 answer
asked Jul 20, 2019 in BI by Vaibhav Ameta (17.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...