1

I'm trying to build a graph which shows two 3D Scatter plots, and I managed to do it using this tutorial.

enter image description here

However, I'm struggling to make the hovertemplate work. I've followed the same structure I used to make it work for a single Scatter 3D plot (built directly, without graph_objects). As far as I know, since the customdata array is as big as the number of points to render, it should make a match 1customdata-to-1voxel, but it's not doing so. In fact, on hover you only see the literal string set in the hovertemplate parameter.

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import json 
import pandas as pd
import numpy as np

json_data = {}
with open("voxels_with_calculated.json", "r") as in_file:
    json_data = json.load(in_file)
json_df = pd.DataFrame(json_data)

#json_df rows look like this
#{
#   "x": 1,
#   "y": -31,
#   "z": -63,
#   "M": -1,
#   "E": -1,
#   "F": -1,
#   "CE": -1,
#   "MA": -1,
#   "R": -1
#}

x,y,z = json_df.x, json_df.y, json_df.z

fig = make_subplots(
    rows=1, 
    cols=2, 
    specs=[[{"type": "scatter3d"}, {"type": "scatter3d"}]],
    subplot_titles=["Original Voxels", "Voxels to World"]
)

fig.add_trace(
    go.Scatter3d(
    x=x,
    y=y,
    z=z,
    customdata=[json_df.M, json_df.E, json_df.F, json_df.CE, json_df.MA, json_df.R],
    hovertemplate="<b> dfdfd %{customdata[0]}</b> ",
    mode='markers'
),
    row=1, col=1
)

fig.add_trace(
    go.Scatter3d(
    x=x,
    y=y,
    z=z,
    customdata=[json_df.M, json_df.E, json_df.F, json_df.CE, json_df.MA, json_df.R],
    hovertemplate="<b> HAHA %{customdata}</b>",
    mode='markers'
),
    row=1, col=2
)

"""
#This works for a Scatter3D created directly, without graph_objects
fig.update_traces(
    hovertemplate="<br>".join([
        "M: %{customdata[0]}",
        "E: %{customdata[1]}",
        "F: %{customdata[2]}",
        "CE: %{customdata[3]}",
        "MA: %{customdata[4]}",
        "R: %{customdata[5]}",
        "U: %{customdata[6]}"
    ])
)
"""


fig.show()

No matter what I try, on hover I only see the literal %{customdata}.

enter image description here

enter image description here

J. Maria
  • 362
  • 3
  • 14

0 Answers0