我在尝试使用Altair Gallery中的以下示例进行操作:https://altair-viz.github.io/gallery/airports_count.html。目前,似乎唯一显示实际计数的方法是通过工具提示,就像示例所示。然而,我正在编写一个静态可视化,如果可以在标记旁边直接显示精确值而无需用户悬停或交互,那将非常有帮助。是否有一种方法可以实现这一点?
import altair as alt
from vega_datasets import data
airports = data.airports.url
states = alt.topo_feature(data.us_10m.url, feature='states')
# US states background
background = alt.Chart(states).mark_geoshape(
fill='lightgray',
stroke='white'
).properties(
width=500,
height=300
).project('albersUsa')
# airport positions on background
base = alt.Chart(airports).transform_aggregate(
latitude='mean(latitude)',
longitude='mean(longitude)',
count='count()',
groupby=['state']
).encode(
longitude='longitude:Q',
latitude='latitude:Q',
)
points = base.mark_circle().encode(
size=alt.Size('count:Q', title='Number of Airports'),
color=alt.value('steelblue'),
tooltip=['state:N','count:Q']
).properties(
title='Number of airports in US'
)
text = base.mark_text(
dx=15, dy=10
).encode(
text='count:Q'
)
background + points + text
长期来看,更好的解决方案是使用 vega-label,一旦它成为 Vega-Lite 包的一部分,它将能够自动执行此操作。对于 Altair,此功能在此错误中跟踪:https://github.com/altair-viz/altair/issues/1731。