【问题标题】:Retain tooltip of previous layer in Altair在 Altair 中保留上一层的工具提示
【发布时间】:2019-12-07 10:09:13
【问题描述】:

我在 altair 中的 choropleth 图上添加状态轮廓叠加
我的等值线图有它的工具提示。
当我在等值线顶部分层状态轮廓时,我失去了绘图的工具提示功能
有人对如何处理这个有想法吗?
任何帮助将不胜感激

import altair as alt

# saving data into a file rather than embedding into the chart
alt.data_transformers.enable('json') 
alt.renderers.enable('notebook')
# alt.renderers.enable('jupyterlab')
from vega_datasets import data
import pandas as pd
from altair import Scale,Color

states = alt.topo_feature(data.us_10m.url, 'states')
counties = alt.topo_feature(data.us_10m.url+'#', 'counties')

dummy='#dbe9f6'
scheme='blues'
type1='linear'

fg = alt.Chart(counties).mark_geoshape(
        stroke='black',
        strokeWidth=0.05
    ).project(
        type='albersUsa'
    ).transform_lookup(
        lookup='id',
        from_=alt.LookupData(fdf, 'fips', ['year','Pill_per_pop','BUYER_COUNTY', 'state'])
    ).transform_calculate(
        Pill_per_pop='isValid(datum.Pill_per_pop) ? datum.Pill_per_pop : -1'  
    ).encode(
        color = alt.condition(
            'datum.Pill_per_pop > 0',
            alt.Color('Pill_per_pop:Q', scale=Scale(scheme=scheme,type=type1)),
            alt.value(dummy)
            ),
        tooltip=['BUYER_COUNTY:N', 'state:N','Pill_per_pop:Q','year:Q']
    ).properties(
        width=700,
        height=400,
        title='Pills per 100k people'
)

outline = alt.Chart(states).mark_geoshape(stroke='black',strokeWidth=0.2).project(
    type='albersUsa'
    )

fg+outline


我的输出


但是我无法找到保留上一层的工具提示的方法,即县级地图

【问题讨论】:

    标签: python vega altair vega-lite


    【解决方案1】:

    我想通了
    定义一个新图表如下:

    fg1 = alt.Chart(counties).mark_geoshape(
            stroke='black',
            strokeWidth=0.05
        ).project(
            type='albersUsa'
        ).transform_lookup(
            lookup='id',
            from_=alt.LookupData(fdf, 'fips', ['year','Pill_per_pop','BUYER_COUNTY', 'state'])
        ).transform_calculate(
            Pill_per_pop='isValid(datum.Pill_per_pop) ? datum.Pill_per_pop : -1'  
        ).encode(        
            tooltip=['BUYER_COUNTY:N', 'state:N','Pill_per_pop:Q','year:Q']
        ).properties(
            width=700,
            height=400,
            title='Pills per 100k people'
    )
    
    fg+outline+fg1
    

    只需使用工具提示而不是颜色的编码再次定义图表,然后将其分层放在上面的图表之上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 2019-02-23
      • 2010-10-28
      • 1970-01-01
      相关资源
      最近更新 更多