Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Altair choropleth map legend scale and tooltip

I am trying to make the tool tip 'Percentage' be an actual percent and not a decimal. Even when I include alt.Tooltip('Percentage:Q',format='.2%'), it doesn't seem to work.

Also, I am trying to make the legend scale from 0-100% instead of 40-70%.

Any help would be appreciated!

import altair as alt
from vega_datasets import data
states = alt.topo_feature(data.us_10m.url, 'states')
variable_list = ['Percentage', 'State Name', 'state_id']

alt.Chart(states).mark_geoshape().encode(
color=alt.Color('Percentage:Q', title='Positive NFB', legend=alt.Legend(format=".0%"), scale=alt.Scale(scheme='yellowgreen')),
tooltip=['State Name:N', 'Percentage:Q', alt.Tooltip('Percentage:Q',format='.2%')]).properties(title="Percentage of People in Households with Positive NFB"
).transform_lookup(
lookup='id',
from_=alt.LookupData(states_positive_NFB, 'state_id', variable_list)
).properties(
width=500,
height=300
).project(
type='albersUsa'
)

Current map: enter image description here

like image 454
Morgan McCue Avatar asked Oct 15 '25 12:10

Morgan McCue


1 Answers

  • To change the domain of the color scale, you can pass the domain argument to alt.Scale(): e.g.

    alt.Scale(scheme='yellowgreen', domain=[0, 1])
    
  • To make the tooltip format appear, you can remove the duplicated tooltip encoding, as the first one appears to be taking precedence. That is, rather than

    tooltip=['State Name:N', 'Percentage:Q', alt.Tooltip('Percentage:Q',format='.2%')]
    

    you should use

    tooltip=['State Name:N', alt.Tooltip('Percentage:Q', format='.2%')]
    
like image 127
jakevdp Avatar answered Oct 19 '25 09:10

jakevdp