I’m trying to visualize the act of placing a sequence of boxes in a container. I’m new to plotly (we’re trying to migrate from matplotlib) and have been writing code based on discussions I found in this forum. I currently have a chart with a slider but I noticed that the plot doesn’t go back to an earlier frame. My code goes like this:
import plotly.graph_objects as go
dimensions = [5.9, 2.39, 2.39]
x = [0, dimensions[0], dimensions[0], 0, 0, dimensions[0], dimensions[0], 0]
y = [0, 0, dimensions[1], dimensions[1], 0, 0, dimensions[1], dimensions[1]]
z = [0, 0, 0, 0, dimensions[2], dimensions[2], dimensions[2], dimensions[2]]
container = go.Mesh3d(
x=x,
y=y,
z=z,
opacity=0.5,
color="rgba(0,0,255,0)",
name="Container",
)
box1 = {
"x": [0, 0.5, 0.5, 0, 0, 0.5, 0.5, 0],
"y": [0, 0, 1, 1, 0, 0, 1, 1],
"z": [0, 0, 0, 0, 0.3, 0.3, 0.3, 0.3],
"color": "red",
}
box2 = {
"x": [0, 0.6, 0.6, 0, 0, 0.6, 0.6, 0],
"y": [0, 0, 0.6, 0.6, 0, 0, 0.6, 0.6],
"z": [0.3, 0.3, 0.3, 0.3, 1.3, 1.3, 1.3, 1.3],
"color": "blue",
}
box3 = {
"x": (0, 0.5, 0.5, 0, 0, 0.5, 0.5, 0),
"y": (1, 1, 1.8, 1.8, 1, 1, 1.8, 1.8),
"z": (0, 0, 0, 0, 0.3, 0.3, 0.3, 0.3),
"color": "green",
}
boxes = [box1, box2, box3]
frames = []
fig = go.Figure(data=[container] * (len(boxes) + 1))
frames.append(
go.Frame(
data=[container],
traces=[0],
name="frame0",
)
)
for i, box in enumerate(boxes, 1):
frame = go.Frame(
data=[
go.Mesh3d(
x=box["x"],
y=box["y"],
z=box["z"],
opacity=1,
color=box["color"],
alphahull=0,
name=f"Box {i}",
)
],
traces=[i],
name=f"frame{i}",
)
frames.append(frame)
fig.frames = frames
sliders = [
dict(
steps=[
dict(
method="animate",
args=[
[f"frame{k}"],
dict(
mode="immediate",
frame=dict(duration=400, redraw=True),
transition=dict(duration=0),
),
],
label=f"{k+1}",
)
for k in range(len(boxes) + 1)
],
active=0,
transition=dict(duration=0),
x=0,
y=0,
currentvalue=dict(
font=dict(size=12), prefix="step: ", visible=True, xanchor="center"
),
len=1.0,
)
]
fig.update_layout(
scene=dict(
xaxis=dict(range=[0, dimensions[0]], showgrid=False),
yaxis=dict(range=[0, dimensions[1]], showgrid=False),
zaxis=dict(range=[0, dimensions[2]], showgrid=False),
),
updatemenus=[
dict(
type="buttons",
showactive=False,
buttons=[dict(label="Play", method="animate", args=[None])],
)
],
sliders=sliders,
)
I would like for the sliders to allow one to view the boxes placed so far in any selected frame.