[英]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 獲得的滑塊和步驟。
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.