给Altair Choropleth地图添加标签

3

有没有一种方法可以在州地图上添加标签?例如,直接将百分比添加到各个州而不仅仅通过工具提示来查看它们?

我不确定Altair是否有这个选项,或者是否需要研究其他软件包(plotly、folium等)并进行一些分层操作以获得这些结果。

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:

1个回答

3

Altair没有内置功能来显示地理区域的标签。不过,如果您有一个包含纬度、经度和文本列的数据框,并希望进行显示,您可以使用mark_text()latitude/longitude 编码来实现;您可以在Altair的文档中找到一个示例:https://altair-viz.github.io/gallery/london_tube.html

如果您想要一个可视化工具,具有计算地理区域质心并在这些点上绘制标签的内置功能,那么Altair可能并不是您正在寻找的工具。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接