【问题标题】:altair: mark_geoshape tooltips disabled when adding additional chart with selectionaltair:添加带有选择的附加图表时禁用 mark_geoshape 工具提示
【发布时间】:2020-02-16 09:22:34
【问题描述】:

我有一个 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
)

...到地图_图表。

这个例子不是很干净,因为我没有弄清楚如何对选择进行分类,但是它显示了问题的要点和解决方案:

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

【问题讨论】:

  • 您能否提供一个reproducible example 来显示问题?
  • 当我在处理一个可重现的例子时,我意识到我只需要将“.add_selection(bland_selection)”添加到 map_-chart。

标签: python tooltip altair


【解决方案1】:

OP 通过根据他们的评论将.add_selection(bland_selection) 添加到地图中解决了这个问题。

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多