如何重新排序自定义悬停标签外观的度量?

kos*_*nov 6 python plotly plotly-express

我正在制作交互式地图,地图本身没有问题,元素位于附加悬停标记中的方式有​​问题。有什么办法可以改变这个顺序吗?是否可以不显示纬度和经度指示器?

示例代码:

@st.cache(hash_funcs={dict: lambda _: None})
def my_stat_map(df_region_map):

    fig_map = px.scatter_mapbox(df_region_map,
                                hover_name='Region name',
                                hover_data=['Confirmed', 'Deaths', 'Recovered',
                                            'Daily confirmed', 'Daily deaths', 'Daily recovered'],
                                lat='Latitude dd',
                                lon='Longitude dd',
                                size='Confirmed',
                                color='Confirmed',
                                color_continuous_scale='Sunsetdark',
                                zoom=3,
                                size_max=45,
                                opacity=0.8,
                                height=600)

    fig_map.update_layout(mapbox_style="carto-positron", showlegend=True)
    fig_map.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})

    dict_map = {'map_key': fig_map}
    return dict_map
Run Code Online (Sandbox Code Playgroud)

我能得到什么: 在此输入图像描述

如何更改此输出中的顺序?我想删除纬度和经度,或者至少将它们移到输出的末尾。

Rob*_*ond 2

这里介绍了这一点:https://plotly.com/python/hover-text-and-formatting/#disabling-or-customizing-hover-of-columns-in-plotly-express



fig_map = px.scatter_mapbox(
    df_region_map,
    hover_name="Region name",
    hover_data={
        "Confirmed":True,
        "Deaths":True,
        "Recovered":True,
        "Daily confirmed":True,
        "Daily deaths":True,
        "Daily recovered":True,
        "Latitude dd":False,
        "Longitude dd":False
    },
    lat="Latitude dd",
    lon="Longitude dd",
    size="Confirmed",
    color="Confirmed",
    color_continuous_scale="Sunsetdark",
    zoom=3,
    size_max=45,
    opacity=0.8,
    height=600,
)

fig_map.update_layout(mapbox_style="carto-positron", showlegend=True)
fig_map.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})

fig_map
Run Code Online (Sandbox Code Playgroud)

创建数据框

import plotly.express as px
import pandas as pd
import requests

df_ = pd.read_csv(
    "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/latest/owid-covid-latest.csv"
)
# alpha3
df_geo = pd.json_normalize(
    requests.get(
        "https://raw.githubusercontent.com/eesur/country-codes-lat-long/master/country-codes-lat-long-alpha3.json"
    ).json()["ref_country_codes"]
).rename(columns={"latitude": "Latitude dd", "longitude": "Longitude dd"})

df = df_.loc[
    :,
    [
        "iso_code",
        "location",
        "total_cases",
        "total_deaths",
        "total_tests",
        "new_cases",
        "new_deaths",
        "new_tests",
    ],
].rename(
    columns={
        "location": "Region name",
        "total_cases": "Confirmed",
        "total_deaths": "Deaths",
        "total_tests": "Recovered",
        "new_cases": "Daily confirmed",
        "new_deaths": "Daily deaths",
        "new_tests": "Daily recovered",
    }
)
df_region_map = pd.merge(df, df_geo, left_on="iso_code", right_on="alpha3").dropna(subset="Confirmed")


Run Code Online (Sandbox Code Playgroud)