使用 plotly express 以动画形式显示图像和绘图
Using plotly express to show image and plot with animation
我需要一些帮助来制作带有 plotly express 的动画,其中每一帧都将并排包含图像和图形(最好是 plotly express 图形)。我的目标是让动画与每一帧的时间步长相对应。 Plotly Express 有一个这样的例子,只有下面 link 中显示的图像。但是我不知道如何让一个 plotly express graph 代替示例中的一个图像。
下面的 link 将带您进入“结合动画和构面”的情节表达示例
Link 为具体示例绘制:'Combining animations and facets' (https://plotly.com/python/imshow/)
'''
from skimage import data, io
import plotly.express as px
data = io.imread("https://github.com/scikit-image/skimage-tutorials/raw/main/images/cells.tif")
data = data.reshape((15, 4, 256, 256))[5:]
fig = px.imshow(data, animation_frame=0, facet_col=1, binary_string=True)
fig.show()
'''
更新:
所以我可以用图像制作我的侧面图,但我很难制作它的动画。以下是我当前的代码:
'''
imgR=np.random.rand(360,1000)
imgR2=np.random.rand(360,1000)
fig = make_subplots(rows=1, cols=2)
fig.add_trace(
go.Scatter(x=[1, 2, 3,4,5], y=[4, 5, 6,7,8]),
row=1, col=1
)
fig.add_trace(
px.imshow(imgR).data[0],
row=1, col=2
)
frames=[go.Frame(data=[go.Scatter(x=[3,5,2,5,3],y=[3,4,5,3,3]),px.imshow(imgR2).data[0]],traces=[0,1])]
fig.frames=frames
button = dict(
label='Play',
method='animate',
args=[None, dict(frame=dict(duration=50, redraw=False),
transition=dict(duration=0),
fromcurrent=True,
mode='immediate')])
fig.update_layout(updatemenus=[dict(type='buttons',
showactive=False,
y=0,
x=1.05,
xanchor='left',
yanchor='bottom',
buttons=[button] )
],
width=800, height=500)
fig.update_layout(height=600, width=800, title_text="Side By Side Subplots")
fig.show()
'''
任何帮助将不胜感激。
您需要系统地处理许多事情
- 确保每一帧都有一个名称
- 确保每条轨迹都使用正确的轴
- 从框架列表构建滑块
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import numpy as np
imgR = np.random.rand(20, 40)
fig = make_subplots(rows=1, cols=2)
x = [1, 2, 3, 4, 5]
y = [4, 5, 6, 7, 8]
fig.add_trace(go.Scatter(x=[1, 2, 3, 4, 5], y=[4, 5, 6, 7, 8]), row=1, col=1)
fig.add_trace(px.imshow(imgR).data[0], row=1, col=2)
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=[
go.Scatter(x=x, y=y),
px.imshow(np.random.rand(20, 40) if i > 0 else imgR)
.update_traces(xaxis="x2", yaxis="y2")
.data[0],
],
)
)
# 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
],
}
],
)
我需要一些帮助来制作带有 plotly express 的动画,其中每一帧都将并排包含图像和图形(最好是 plotly express 图形)。我的目标是让动画与每一帧的时间步长相对应。 Plotly Express 有一个这样的例子,只有下面 link 中显示的图像。但是我不知道如何让一个 plotly express graph 代替示例中的一个图像。
下面的 link 将带您进入“结合动画和构面”的情节表达示例
Link 为具体示例绘制:'Combining animations and facets' (https://plotly.com/python/imshow/)
'''
from skimage import data, io
import plotly.express as px
data = io.imread("https://github.com/scikit-image/skimage-tutorials/raw/main/images/cells.tif")
data = data.reshape((15, 4, 256, 256))[5:]
fig = px.imshow(data, animation_frame=0, facet_col=1, binary_string=True)
fig.show()
'''
更新:
所以我可以用图像制作我的侧面图,但我很难制作它的动画。以下是我当前的代码:
'''
imgR=np.random.rand(360,1000)
imgR2=np.random.rand(360,1000)
fig = make_subplots(rows=1, cols=2)
fig.add_trace(
go.Scatter(x=[1, 2, 3,4,5], y=[4, 5, 6,7,8]),
row=1, col=1
)
fig.add_trace(
px.imshow(imgR).data[0],
row=1, col=2
)
frames=[go.Frame(data=[go.Scatter(x=[3,5,2,5,3],y=[3,4,5,3,3]),px.imshow(imgR2).data[0]],traces=[0,1])]
fig.frames=frames
button = dict(
label='Play',
method='animate',
args=[None, dict(frame=dict(duration=50, redraw=False),
transition=dict(duration=0),
fromcurrent=True,
mode='immediate')])
fig.update_layout(updatemenus=[dict(type='buttons',
showactive=False,
y=0,
x=1.05,
xanchor='left',
yanchor='bottom',
buttons=[button] )
],
width=800, height=500)
fig.update_layout(height=600, width=800, title_text="Side By Side Subplots")
fig.show()
'''
任何帮助将不胜感激。
您需要系统地处理许多事情
- 确保每一帧都有一个名称
- 确保每条轨迹都使用正确的轴
- 从框架列表构建滑块
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import numpy as np
imgR = np.random.rand(20, 40)
fig = make_subplots(rows=1, cols=2)
x = [1, 2, 3, 4, 5]
y = [4, 5, 6, 7, 8]
fig.add_trace(go.Scatter(x=[1, 2, 3, 4, 5], y=[4, 5, 6, 7, 8]), row=1, col=1)
fig.add_trace(px.imshow(imgR).data[0], row=1, col=2)
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=[
go.Scatter(x=x, y=y),
px.imshow(np.random.rand(20, 40) if i > 0 else imgR)
.update_traces(xaxis="x2", yaxis="y2")
.data[0],
],
)
)
# 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
],
}
],
)