繁体   English   中英

如何使用 imshow 创建带有动画图像的子图和使用 plotly express 创建非动画图像

[英]How to create a subplot with an animated image using imshow and a non animated image using plotly express

我目前正在开展一个项目,使用 plotly express 的 imshow 显示图像的过渡。 我正在尝试创建一个子图,但很难将当前的动画图与另一个非动态图放置在一起。 有没有人有任何指示。 请参阅下图以了解我目前所拥有的内容以及下面的代码以重现它。

from random import randint
import plotly.express as px
from plotly.subplots import make_subplots


class Grid:
    def __init__(self, rows, columns):
        self.rows = rows
        self.cols = columns

    def generate_starting_population(self):
        x = np.array([[[randint(0, 255), randint(0, 255), randint(0,255)] for i in range(self.rows)] for j in range(self.cols)], dtype = np.uint8)

        return x


if __name__ == "__main__":


    populations = []
    grid = Grid(64, 64)

    for i in range(64):
        populations.append(grid.generate_starting_population())


    populations = np.array(populations)

    fig = px.imshow(populations, animation_frame =0)
    # fig = px.imshow(populations, animation_frame=0)
    # fig.add_traces(px.imshow(populations[0]), row = 1, col = 1)
    fig.update_yaxes(visible=False, showticklabels=False)
    fig.update_xaxes(visible=False, showticklabels=False)
    fig.update_layout(sliders=[{"currentvalue": {"prefix": "Current Generation="}}])
    fig.show()

这是此代码当前生成的图像,但我想在当前图像旁边放置一个非动画图。

在此处输入图像描述

在使用图形对象构建子图后,左图是动画的,右图是静态的,方法是创建一个新帧并更新图形,同时重用 Express 获得的滑块和步骤。

  1. 在 px.imshow 中创建对象
  2. 重用滑块和菜单
  3. 为动画和静止图像重新创建帧
  4. 框架、菜单和滑块更新

from random import randint
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

class Grid:
    def __init__(self, rows, columns):
        self.rows = rows
        self.cols = columns

    def generate_starting_population(self):
        x = np.array([[[randint(0, 255), randint(0, 255), randint(0,255)] for i in range(self.rows)] for j in range(self.cols)], dtype = np.uint8)

        return x

if __name__ == "__main__":

    populations = []
    grid = Grid(64, 64)

    for i in range(64):
        populations.append(grid.generate_starting_population())


    populations = np.array(populations)

    fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.01)
    
    fig_px = px.imshow(populations, animation_frame=0)

    sliders = fig_px.layout.sliders
    updatemenus = fig_px.layout.updatemenus
    frames =[go.Frame(data=[go.Image(z=populations[k], visible=True, name=str(k)),
                            go.Image(z=populations[1])],
                      traces=[0,1], name=str(k)) for k in range(populations.shape[0])]

    fig.add_trace(go.Image(z=populations[0]), row=1, col=1)
    fig.add_trace(go.Image(z=populations[1]), row=1, col=2)
    
    fig.update_yaxes(visible=False, showticklabels=False)
    fig.update_xaxes(visible=False, showticklabels=False)
    
    fig.update(frames=frames)
    fig.update_layout(updatemenus=updatemenus, sliders=sliders)
    fig.update_layout(sliders=[{"currentvalue": {"prefix": "Current Generation="}}])
  
fig.show()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM