我有一个 GeoDataFrame,我将其绘制为 mark_geoshape-map (map_)。颜色和工具提示基于特定列 ('Pkw-Dichte')。到目前为止,这工作正常。
然后我添加了另一个图表(图例),它允许按状态(“平淡”)过滤地图。一切似乎都正常,但现在地图的工具提示已禁用。这是一个错误还是我在这里做错了什么?
可能这个问题不仅与 mark_geoshape 相关,因为我在结合 mark_boxplot 工具提示时遇到了同样的问题。我想这与选择有关。
bland_selection = alt.selection_multi(fields=['bland'])
map_ = alt.Chart(source).mark_geoshape(
).encode(
tooltip=['Pkw-Dichte'],
color='Pkw-Dichte'
).properties(
height=700,
width=500
).transform_filter(
bland_selection
)
legend = alt.Chart(source).mark_rect(
).encode(
y=alt.Y('bland:N', axis=alt.Axis(title='State', orient='right')),
color=alt.condition(bland_selection,
alt.value('gray'),
alt.value('lightgray')),
tooltip=['bland:N'],
).add_selection(
bland_selection
)
map_ | legend
编辑:
这是一个可重现的示例,包括解决方案,添加:
.add_selection(
bland_selection
)
...到map_图表。
这个例子不是很干净,因为我没有弄清楚如何对选择进行分类,但是它显示了问题的要点和解决方案:
import altair as alt
from vega_datasets import data
counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url
bland_selection = alt.selection_multi(fields=['id'])
map_=alt.Chart(counties).mark_geoshape().encode(
color='rate:Q',
tooltip=['rate:Q']
).transform_lookup(
lookup='id',
from_=alt.LookupData(source, 'id', ['rate'])
).project(
type='albersUsa'
).properties(
width=500,
height=300
).transform_filter(
bland_selection
).add_selection(
bland_selection
)
chart=alt.Chart(counties).mark_rect().encode(
y= alt.Y("id:O", bin=True),
color=alt.condition(bland_selection,
alt.value('gray'),
alt.value('lightgray')),
tooltip=['rate:Q']
).transform_lookup(
lookup='id',
from_=alt.LookupData(source, 'id', ['rate'])
).add_selection(
bland_selection
)
map_ | chart

