简体   繁体   English

在没有值的情况下自定义显示 map plotly 上的信息

[英]Customized display of information on the map plotly in the absence of values

I map statistical information about monkeypox cases using the plotly library. I map 使用 plotly 库关于猴痘病例的统计信息。 The data that I use does not always contain information about all the metrics interfering with me, which is why I get the following on the map output when hovering over a point:我使用的数据并不总是包含有关干扰我的所有指标的信息,这就是为什么当悬停在一个点上时我在 map output 上得到以下信息:

在此处输入图像描述

Namely, the values %{customdata[N]} .即,值%{customdata[N]} I figured it had to do with the NaN values that are in the data.我认为这与数据中的NaN值有关。 This is how I display the map itself:这就是我显示 map 本身的方式:

@st.cache(hash_funcs={dict: lambda _: None})
def my_stat_map_cases(df_cases_map):
    fig_map = px.scatter_mapbox(df_cases_map,
                                hover_name='ID',
                                hover_data={'Status': True,
                                            'Date confirmation': True,
                                            'Gender': True,
                                            'Symptoms': True,
                                            'Hospitalised (Y/N/NA)': True,
                                            'Location': True,
                                            'City': True,
                                            'Country': True,
                                            'Latitude dd': False, 'Longitude dd': False},
                                lat='Latitude dd',
                                lon='Longitude dd',
                                zoom=3,
                                size_max=100,
                                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

Test option, I'm still figuring out how to do it.测试选项,我还在想办法怎么做。 Is it possible to somehow change the output window so that when I hover over it, if there are empty values, I get no %{customdata[N]} but, say, the line No data available .是否有可能以某种方式更改 output window 以便当我 hover 超过它时,如果有空值,我得到没有%{customdata[N]}行但是,说No data available I don't really like the idea of making such changes directly at the level of the data itself, because.我真的不喜欢直接在数据本身级别进行此类更改的想法,因为。 they can be updated from the resource itself.它们可以从资源本身更新。 Maybe there is some more elegant solution?也许有一些更优雅的解决方案?

A small slice of data from the database:来自数据库的一小部分数据:

在此处输入图像描述

Assume we have a dataset with Nan values as yours:假设我们有一个包含 Nan 值的数据集:

df = px.data.iris()

fig = px.scatter(df, x='petal_length', y='sepal_length')

 # I add some random Nan values to the dataframe
for col in df.columns: 
    df.loc[df.sample(frac=0.1).index, col] = pd.np.nan 

# Fill up the nan values in the dataframe with 'no data' and assign it to customdata
customdata = df.fillna('no data') 

hovertemplate = "<br>".join([
        "petal_length: %{customdata[2]}",
        "sepal_length: %{customdata[0]}"])

fig.update_traces(customdata=customdata, hovertemplate=hovertemplate)

fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM