python-3.x - 使用 Altair 工具提示和 mark_text 显示图形和名称

标签 python-3.x tooltip geopandas altair

以下是我想要在 Altair 图表上显示的工具提示和标签的三个问题。所有的问题或多或少都是有联系的。

  1. 首先,我想修改使用工具提示显示的信息的名称:

年份而不是properties.annee

区域而不是properties.region

生物能源而不是properties.bioenerie...

  • 其次,我想对工具提示中显示的值进行四舍五入。 “11.2”而不是“11.1687087653” 我编写的代码可以满足我在区域中放置的标签的要求,但它不适用于工具提示。

  • 第三,我想在标签和工具提示中显示单位,但我在文档中找不到正确的语法。

  • 下面是我的代码。

    预先感谢您的回答。

    伯特兰

    Current result of my code

        def gen_map(data: gpd.geodataframe.GeoDataFrame, title: str, abs_values: bool):    
            data_json = json.loads(data.to_json())
            choro_data = alt.Data(values=data_json['features'])
    
            # Absolute values or relative values
            if abs_values:
                column = data.columns[0]
                units = 'MW'
                form = '.0f'
            else:
                column = data.columns[1]
                units = '%'
                form = '.1f'
    
            # Base layer
            layer = alt.Chart(choro_data, title=title).mark_geoshape(
                stroke='white',
                strokeWidth=1
            ).encode(
                alt.Color(f'properties.{column}:Q',
                       type='quantitative',
                       title = f'Installed Capacity in {units}'),
            tooltip=[f'properties.annee:Q', 
                     f'properties.region:O', 
                     f'properties.{column}:Q', 
                     alt.Text(f'properties.{column}:Q', format=form)]
             ).transform_lookup(
                lookup='region',
                from_=alt.LookupData(choro_data, 'region')
            ).properties(
                width=600,
                height=500
            )
    
            # Label layer
            labels = alt.Chart(choro_data).mark_text(baseline='top'
                                                 ).properties(
                width=600,
                height=500
            ).encode(
                longitude='properties.centroid_lon:Q',
                latitude='properties.centroid_lat:Q',
                text=alt.Text(f'properties.{column}:Q', format=form),
                size=alt.value(14),
                opacity=alt.value(1)        
            )
    
            return layer + labels
    
        gen_map(bioenergies_2019, 'Bioenergy in France in 2019', False)
    

    最佳答案

    不要使用字符串列表,而是使用 alt.Tooltip 列表对象:

    tooltip=[alt.Tooltip('properties.annee:Q', title='Annee'),
             alt.Tooltip('properties.region:O', title='Region'),
             alt.Tooltip(f'properties.{column}:Q', title=f'{column}')]
    

    您还可以传递format参数来指定值的格式;对于数字格式,请使用 d3-format代码;对于日期/时间格式,请使用 d3-date-format代码。

    关于python-3.x - 使用 Altair 工具提示和 mark_text 显示图形和名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61860404/

    相关文章:

    python - Geopandas PostGIS 连接

    python - 如何在 Windows 10 中安装适用于 Python 3.6 的 openbabel?

    python - 为什么我的 CreateAPIView 不显示产品和商店类别字段?

    javascript - CSS 不适用于自定义工具提示的箭头

    c# - 工具提示或类似的东西在 WPF 中随光标移动

    javascript - 顶部的自定义位置工具提示

    python - 使用plotly绘制geopandas数据框

    python - 将使用 Geopandas、Pandas 和 matplotlib 构建的分区统计图存储到图像文件

    python - np.reshape 返回错误 'ValueError: total size of new array must be unchanged'

    macos - 适用于 Mac OSX 的 IPython3