简体   繁体   中英

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

I am currently working on a project to display the transition of images using plotly express's imshow. I am trying to create a subplot but am having very hard difficulty laying the current animated plot with another one that is not dynamic. Does anyone have any pointers. See the image below for a reference of what I have so far and the code below to reproduce it.

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()

This is an image of what this code currently produces, but I want to put a non-animated plot next to the current one.

在此处输入图像描述

After constructing a subplot with graph objects, the left graph is animated and the right is static by creating a new frame and updating the graph while reusing the sliders and steps obtained by Express.

  1. Creating objects in px.imshow
  2. Reuse of sliders and menus
  3. Re-create frames for animation and still image
  4. Frame, menu, and slider update

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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