Black Lives Matter. Please consider donating to Black Girls Code today.

Hover in Densitymapbox

Hi,
I am kind of new to plotly, I am able to create all maps in plotly with hover. But I am not able to add hover columns to Densitymapbox of graphobjects. Can someone help me on this?

fig = go.Densitymapbox(lat=df[lat], lon=df[lon], z=df[heatmap_category],hovertext=df[hover],text=df[hover],hoverinfo='all',name=legend,showlegend=showlegend,
                                 radius=20,opacity=opacity,colorscale=colormap,colorbar={'x':(0.95)-(0.05*len(figure)),'y':1,'len':0.3,'title':{'text':'<b>'+legend[:4]+'</b>','font':{'size':7}},'thickness':15,'yanchor':'top','tickfont':{'size':6}},
                                showscale=True,)

I am kind of trying this. I am able to create plot but hover is showing only lat-longs and index. I need to show particular column.

Hi @manisekhar,

To display data from a dataframe column, which is not used in the Densitymapbox definition, you have to define that column as a customdata, and specify it in the hovertemplate string, as follows:

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv')

import plotly.graph_objects as go

customdata = df['Date']
hovertemplate = 'lon: %{lon}<br>lat: %{lat}<br>magnit: %{z}<br>date: %{customdata}<extra></extra>'
fig = go.Figure(go.Densitymapbox(lat=df.Latitude, 
                                 lon=df.Longitude, 
                                 z=df.Magnitude, 
                                 customdata=customdata,
                                 hovertemplate=hovertemplate,
                                 radius=10))
fig.update_layout(mapbox_style="stamen-terrain", mapbox_center_lon=180)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()