Hello,
I am having issues with adding custom data to the hover label. I was able to get it working with dummy data as shown below.
However, when plotting within the for loop the hover label says “continent=%{customdata}…”
I also try using other attributes within the Scatter Trace but getting the same behavior (i.e., not showing the data but showing the string as-is.
Any help would be appreciate it
Trial 1 - Dummy Data
fig3 = go.Figure(data = go.Scatter( x = [1,2,3],
y = [2,15,12],
customdata = ['Hola','Hello','Buenos Dias'],
hovertemplate = '(%{x} , %{y} , <b>%{customdata}</b>') )
fig3.show()
Code not working (This is a GO approach to figPX = px.scatter(df_2007, x=“gdpPercap”, y=“lifeExp”, log_x=True, color=‘continent’)
import plotly.express as px
df_2007 = px.data.gapminder().query("year==2007")
df_2007.reset_index(inplace = True, drop = True)
#Create Dictionary for mapping colors
colorMapper = dict(Asia = px.colors.qualitative.Plotly[0],
Europe = px.colors.qualitative.Plotly[1],
Africa = px.colors.qualitative.Plotly[2],
Americas = px.colors.qualitative.Plotly[3],
Oceania = px.colors.qualitative.Plotly[4])
print(colorMapper)
df_2007['colorArray'] = df_2007['continent'].map(colorMapper)
fig_GO = go.Figure() # Create empty figure
#Groupby each Continent
grp = df_2007.groupby('continent', sort = False)
#Iteration to plot each Continent
for key in grp.groups.keys():
fig_GO.add_trace(go.Scatter(
x = df_2007.loc[ grp.groups[key] , 'gdpPercap'], #Indexing by label using Groupby indexes
y = df_2007.loc[ grp.groups[key] , 'lifeExp'],
mode = 'markers',
customdata = [14], # I tried many things here: integer , string ,
# np.random.rand(len(df_2007.loc[ grp.groups[key]]))
marker_color = df_2007.loc[ grp.groups[key] , 'colorArray'],
name = key,
hovertemplate = 'continent=%{customdata}<br>gdpPercap=%{x}<br>lifeExp=%{y}<extra></extra>'
))
# Update Layout & Axes
fig_GO.update_layout(legend_title = 'continent' )
fig_GO.update_xaxes(type = 'log', title = 'gdpPercap' )
fig_GO.update_yaxes(title = 'lifeExp' )
fig_GO.show()