使用 Plotly,我有 2 张并排的图像,我想使用滑块仅更改第一张图像。
但不幸的是,使用滑块时两个图像都被修改了。
这里有完整的示例代码:
import plotly.express as px
import plotly.graph_objects as go
import plotly.subplots as sp
import numpy as np
image1 = np.random.randint(0, 256, size=(100, 100)).astype(np.uint8)
image2 = np.random.randint(0, 256, size=(100, 100)).astype(np.uint8)
image3 = np.random.randint(0, 256, size=(100, 100)).astype(np.uint8)
fig = sp.make_subplots(rows=1, cols=2)
fig.add_trace(px.imshow(image1).data[0], row=1, col=1)
fig.add_trace(px.imshow(image2).data[0], row=1, col=2)
slider = go.layout.Slider(
active=0,
steps=[
{"label": "Image 1", "method": "update", "args": [{"z": [image1]}, {"x": [0], "y": [0]}, {"row":1, "col":1}]},
{"label": "Image 2", "method": "update", "args": [{"z": [image2]}, {"x": [0], "y": [0]}, {"row":1, "col":1}]},
{"label": "Image 3", "method": "update", "args": [{"z": [image3]}, {"x": [0], "y": [0]}, {"row":1, "col":1}]}
]
)
fig.update_layout(
sliders=[slider],
xaxis={"title": "Image 1"}
# xaxis2={"title": "Image 2"}
)
# fig.update_traces(selector={"row":1, "col":1}, sliders=[slider]) # don't work
fig.update_xaxes(matches='x')
fig.update_yaxes(matches='y')
fig.show()
好的,解决方案是使用:
steps=[
{"label": "Image 1", "method": "update", "args": [{"z": [image1,image2]},
{"label": "Image 1", "method": "update", "args": [{"z": [image2,image2]},
{"label": "Image 1", "method": "update", "args": [{"z": [image3,image2]}
]