Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding labels to altair choropleth maps

Is there a way to add labels to the state maps? For example, to have the percentages directly on the individual states instead of only being able to see them via the tool tip?

I'm not sure if Altair has this option or if I will need to look into other packages (plotly, folium etc.) and do some layering to get those results.

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(stroke='white', width=0.01).encode(
    color=alt.Color('Percentage:Q', title='Positive NFB', legend=alt.Legend(format=".0%"), scale=alt.Scale(scheme='yellowgreenblue', domain=[0, 1])),
    tooltip=['State Name:N', alt.Tooltip('Percentage:Q', format='.0%')]).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:

like image 846
Morgan McCue Avatar asked Nov 16 '25 10:11

Morgan McCue


1 Answers

Altair has no built-in features for displaying labels on geographic regions. That said, if you have a dataframe with columns containing latitudes, longitudes, and text that you would like to display, you can do so with a mark_text() and latitude/longitude encodings; you can find an example of this in Altair's documentation: https://altair-viz.github.io/gallery/london_tube.html

If what you want is a visualization tool that has built-in functionality for computing centroids of geographic regions and drawing labels at those points, Altair is probably not what you're looking for.

like image 126
jakevdp Avatar answered Nov 19 '25 11:11

jakevdp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!