无论如何,在Altair中是否会弹出部分地图或更改颜色

时间:2019-06-02 10:06:32

标签: python altair

我正在尝试在altair中绘制印度州。我能够绘制地图并在工具提示中出现州名。我希望该州在选择时弹出或更改颜色。是否有任何方法可以做到这一点

我尝试使用selection_interval。但由于我是新手,所以无法做到

'''python

import altair as alt

url = "https://raw.githubusercontent.com/deldersveld/topojson/master/countries/india/india-states.json"

source = alt.topo_feature(url, "IND_adm1")

alt.Chart(source).mark_geoshape().encode(
    tooltip='properties.NAME_1:N',
    color=alt.value('lightgray')   

).properties(
        width=800,
        height=500

)

1 个答案:

答案 0 :(得分:2)

您可以使用带有条件颜色的Single Selection来执行以下操作:

import altair as alt

url = "https://raw.githubusercontent.com/deldersveld/topojson/master/countries/india/india-states.json"

source = alt.topo_feature(url, "IND_adm1")
hover = alt.selection_single(on='mouseover', empty='none')

alt.Chart(source).mark_geoshape().encode(
    tooltip='properties.NAME_1:N',
    color=alt.condition(hover, alt.value('steelblue'), alt.value('lightgray'))
).properties(
    width=800,
    height=500
).add_selection(
    hover
)

enter image description here