带有图像的情节动画非常慢。可以做哪些优化?

发布于 2025-01-14 13:22:47 字数 2356 浏览 2 评论 0原文

谁能帮我理解为什么帧播放速度这么慢?这只是我想要实现超过 100 个带有数据点的图像的过程的示例。当我添加播放按钮时,它将无法在每一帧中足够快地加载图像,但散点会针对每一帧进行精细更新。即使手动移动滑块,加载图像组件也会出现相当多的延迟。 有没有更有效的方法来创建这种类型的动画?

import numpy as np
import plotly
import plotly.express as px
import plotly.graph_objects as go

imgR = np.random.rand(360, 700)
x=np.random.randint(200,size=10)
y=np.random.randint(200,size=10)

fig=px.imshow(imgR,
              color_continuous_scale='gray',
              origin='lower'
              )
fig.add_trace(go.Scatter(name='Flame Front',
                         x=x, 
                         y=y,
                         mode='markers'                  
                         )
)


frames = []
for i in range(5):
    if i > 0:
        np.random.shuffle(x)
        np.random.shuffle(y)
    # need to name the frame for slider, plus make sure both traces are using
    # correct axes
    frames.append(
        go.Frame(
            name=str(i),
            data=[
                px.imshow(np.random.rand(360, 700) if i > 0 else imgR).data[0],
                go.Scatter(x=x, y=y)
            ],
        )
    )




# now have all parts to puild figure
figa = go.Figure(data=fig.data, frames=frames, layout=fig.layout)

# add slider
figa.update_layout(
    sliders=[
        {
            "active": 0,
            "currentvalue": {"prefix": "animation_frame="},
            "len": 0.9,
            "steps": [
                {
                    "args": [
                        [fr.name],
                        {
                            "frame": {"duration": 0, "redraw": True},
                            "mode": "immediate",
                            "fromcurrent": True,
                        },
                    ],
                    "label": fr.name,
                    "method": "animate",
                }
                for fr in figa.frames
            ],
        }
    ],
)

figa.update_layout(
    showlegend=True,
    title="Plot Title",
    xaxis_title="X Axis",
    yaxis_title="Y Axis",
    legend_title="Legend Title",
    legend=dict(
        xanchor='right',
        yanchor='top',
        traceorder="reversed",
        title_font_family="Times New Roman",
        font=dict(
            family="Courier",
            size=12,
            color="black"
        )
    )
)

figa.show()

Can anyone help me understand why the frame playthrough is so slow? This is just an example of a process I want to implement over 100s of images with data points. When I did add the play button it would not be able to load the image fast enough through each frame, but the scatter points would update for each frame fine. Even moving the slider manually shows quite a bit of delay to load the image component.
Is there a more efficient way with plotly to create this type of animation?

import numpy as np
import plotly
import plotly.express as px
import plotly.graph_objects as go

imgR = np.random.rand(360, 700)
x=np.random.randint(200,size=10)
y=np.random.randint(200,size=10)

fig=px.imshow(imgR,
              color_continuous_scale='gray',
              origin='lower'
              )
fig.add_trace(go.Scatter(name='Flame Front',
                         x=x, 
                         y=y,
                         mode='markers'                  
                         )
)


frames = []
for i in range(5):
    if i > 0:
        np.random.shuffle(x)
        np.random.shuffle(y)
    # need to name the frame for slider, plus make sure both traces are using
    # correct axes
    frames.append(
        go.Frame(
            name=str(i),
            data=[
                px.imshow(np.random.rand(360, 700) if i > 0 else imgR).data[0],
                go.Scatter(x=x, y=y)
            ],
        )
    )




# now have all parts to puild figure
figa = go.Figure(data=fig.data, frames=frames, layout=fig.layout)

# add slider
figa.update_layout(
    sliders=[
        {
            "active": 0,
            "currentvalue": {"prefix": "animation_frame="},
            "len": 0.9,
            "steps": [
                {
                    "args": [
                        [fr.name],
                        {
                            "frame": {"duration": 0, "redraw": True},
                            "mode": "immediate",
                            "fromcurrent": True,
                        },
                    ],
                    "label": fr.name,
                    "method": "animate",
                }
                for fr in figa.frames
            ],
        }
    ],
)

figa.update_layout(
    showlegend=True,
    title="Plot Title",
    xaxis_title="X Axis",
    yaxis_title="Y Axis",
    legend_title="Legend Title",
    legend=dict(
        xanchor='right',
        yanchor='top',
        traceorder="reversed",
        title_font_family="Times New Roman",
        font=dict(
            family="Courier",
            size=12,
            color="black"
        )
    )
)

figa.show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文