当使用plotlyexpressimshow和facet_col时,返回的图形有多个轨迹。如何将该图形正确添加到Figurewidget 对象并随后使用新数据进行更新? 这是我尝试过但不起作用的原因:
Exception: In order to reference traces by row and column, you must first use plotly.tools.make_subplots to create the figure with a subplot grid.
px_fig = px.imshow(
imgs[:, :, :],
binary_string=False,
zmin=0,
zmax=1,
facet_col=0,
aspect="auto",
facet_col_wrap=20,
facet_row_spacing=0.02,
facet_col_spacing=0.005,
color_continuous_scale='jet',
)
o_samplesviz = go.FigureWidget()
subplots = make_subplots(rows=len(rows), cols=len(col_list))
subplots.add_traces(data=px_fig.data, rows=row_list, cols=col_list)
o_samplesviz.add_traces(subplots.data, rows=row_list, cols=col_list)
目标是创建一个热图网格,这就是 imshow 图返回的内容,将该网格放入 go.Figurewidget 对象中,以便我可以将其与 ipywidgets 集成。
谢谢
您的错误消息在 Plotly Github 1 和 Plotly Github 2 上提到。他们合并了它,但它仍然发生。解决方法是将
rows
和 cols
的输入从列表更改为整数,因为这是导致异常的原因。
一旦你解决了这个问题,你就会陷入异常
Exception: In order to reference traces by row and column, you must first use plotly.tools.make_subplots to create the figure with a subplot grid.
这也被提到了here。通过向 go.FigureWidget
添加子图即可解决。
下面是您的代码的 MWE:
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
imgs = np.array([[[0]],[[0]],[[0]]])
rows = [1,1,1]
row_list = [1,1,1]
col_list = [1,1,1]
px_fig = px.imshow(
imgs[:, :, :],
binary_string=False,
zmin=0,
zmax=1,
facet_col=0,
aspect="auto",
facet_col_wrap=20,
facet_row_spacing=0.02,
facet_col_spacing=0.005,
color_continuous_scale='jet',
)
o_samplesviz = go.FigureWidget(make_subplots(rows=len(rows), cols=len(col_list)))
subplots = make_subplots(rows=len(rows), cols=len(col_list))
subplots.add_traces(data=px_fig.data, rows=len(row_list), cols=len(col_list))
o_samplesviz.add_traces(subplots.data, rows=len(row_list), cols=len(col_list))
o_samplesviz.show(renderer="svg")