Customized display of information on the map plotly in the absence of values
Question:
I map statistical information about monkeypox cases using the plotly library. 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:
Namely, the values %{customdata[N]}
. I figured it had to do with the NaN
values that are in the data. This is how I display the map itself:
@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
. 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:
Answers:
Assume we have a dataset with Nan values as yours:
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'.
# Assign it to customdata to preserve the original dataframe.
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()
I map statistical information about monkeypox cases using the plotly library. 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:
Namely, the values %{customdata[N]}
. I figured it had to do with the NaN
values that are in the data. This is how I display the map itself:
@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
. 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:
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'.
# Assign it to customdata to preserve the original dataframe.
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()