如何添加滑块来更改 Plotly 图的一个子图?

问题描述 投票:0回答:1

使用 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()
python plotly
1个回答
0
投票

好的,解决方案是使用:

    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]}
    ]
© www.soinside.com 2019 - 2024. All rights reserved.