Altair:geo_shape 不适用于选择
Altair: geo_shape doesn't work with selection
我正在制作一张 Choropleth 地图,它将显示不同州之间的相似之处。因此,当您 select 从下拉列表中选择一个州时,地图将显示它与其他州的相似性。
为此,我使用了 2 个数据集:
- DatasetA:一个长格式数据框,有 3 列:状态 1、状态 2 以及它们之间的相似度。
- DatasetB: 包含每个州几何形状的 GeoDataFrame。
当我尝试在没有 selection 的情况下绘制它时,它起作用了:
alt.Chart(gdf).mark_geoshape(
).encode(
color='Similarity:O',
tooltip = ['Similarity:Q']
).properties(
projection={'type': 'albersUsa'},
width=700,
height=400
).transform_lookup(
lookup='State',
from_=alt.LookupData(source, 'State', source.columns.values)
)
但是一旦我添加了 selection,那么它仅在我 select 怀俄明州(数据集 A 中的最后一个州)时有效。当我select其他状态时,剧情消失
input_dropdown = alt.binding_select(options=source.State.unique())
selection = alt.selection_single(fields=['Similarity_to'], bind=input_dropdown ,init={'Similarity_to': 'New York'})
alt.Chart(gdf).mark_geoshape(
).encode(
color='Similarity:Q',
tooltip = ['Similarity:Q']
).properties(
projection={'type': 'albersUsa'},
width=700,
height=400
).transform_lookup(
lookup='State',
from_=alt.LookupData(source, 'State', source.columns.values)
).transform_filter(
selection
).add_selection(
selection
)
这是一个演示它的剪辑:https://www.loom.com/share/292e8b1a80344cf5a998a54f453ece2c
我使用 transform_fold 让这个工作。问题是 transform_lookup 只匹配一次,所以如果数据集中有多个匹配项,它会忽略它们。所以你必须使用宽格式数据集,然后使用 transform_fold
将其转换回长格式。
input_dropdown = alt.binding_select(options=source.State.unique())
selection = alt.selection_single(fields=['Similarity_to'], bind=input_dropdown ,init={'Similarity_to': 'New York'})
alt.Chart(gdf).mark_geoshape(
stroke='black'
).encode(
color='Similarity:Q',
tooltip = ['Similarity:Q']
).properties(
projection={'type': 'albersUsa'},
width=700,
height=400
).transform_lookup(
lookup='State',
from_=alt.LookupData(source, 'State', source.columns.values)
).transform_fold(
source.drop('State',axis=1).columns.values, # Preserve the State column, fold the rest
['Similarity_to','Similarity']
).transform_filter(
selection
).add_selection(
selection
)
在问这个问题之前我真的试过了。但事实证明我以错误的顺序进行了转换。转换顺序很重要!
我正在制作一张 Choropleth 地图,它将显示不同州之间的相似之处。因此,当您 select 从下拉列表中选择一个州时,地图将显示它与其他州的相似性。
为此,我使用了 2 个数据集:
- DatasetA:一个长格式数据框,有 3 列:状态 1、状态 2 以及它们之间的相似度。
- DatasetB: 包含每个州几何形状的 GeoDataFrame。
当我尝试在没有 selection 的情况下绘制它时,它起作用了:
alt.Chart(gdf).mark_geoshape(
).encode(
color='Similarity:O',
tooltip = ['Similarity:Q']
).properties(
projection={'type': 'albersUsa'},
width=700,
height=400
).transform_lookup(
lookup='State',
from_=alt.LookupData(source, 'State', source.columns.values)
)
但是一旦我添加了 selection,那么它仅在我 select 怀俄明州(数据集 A 中的最后一个州)时有效。当我select其他状态时,剧情消失
input_dropdown = alt.binding_select(options=source.State.unique())
selection = alt.selection_single(fields=['Similarity_to'], bind=input_dropdown ,init={'Similarity_to': 'New York'})
alt.Chart(gdf).mark_geoshape(
).encode(
color='Similarity:Q',
tooltip = ['Similarity:Q']
).properties(
projection={'type': 'albersUsa'},
width=700,
height=400
).transform_lookup(
lookup='State',
from_=alt.LookupData(source, 'State', source.columns.values)
).transform_filter(
selection
).add_selection(
selection
)
这是一个演示它的剪辑:https://www.loom.com/share/292e8b1a80344cf5a998a54f453ece2c
我使用 transform_fold 让这个工作。问题是 transform_lookup 只匹配一次,所以如果数据集中有多个匹配项,它会忽略它们。所以你必须使用宽格式数据集,然后使用 transform_fold
将其转换回长格式。
input_dropdown = alt.binding_select(options=source.State.unique())
selection = alt.selection_single(fields=['Similarity_to'], bind=input_dropdown ,init={'Similarity_to': 'New York'})
alt.Chart(gdf).mark_geoshape(
stroke='black'
).encode(
color='Similarity:Q',
tooltip = ['Similarity:Q']
).properties(
projection={'type': 'albersUsa'},
width=700,
height=400
).transform_lookup(
lookup='State',
from_=alt.LookupData(source, 'State', source.columns.values)
).transform_fold(
source.drop('State',axis=1).columns.values, # Preserve the State column, fold the rest
['Similarity_to','Similarity']
).transform_filter(
selection
).add_selection(
selection
)
在问这个问题之前我真的试过了。但事实证明我以错误的顺序进行了转换。转换顺序很重要!