首页 > 解决方案 > 一种在 for 循环中为每个子图更新图形布局的方法 (Plotly)

问题描述

有没有办法可以像这样循环更新每个图形的布局?我将每个布局添加到列表中,并循环遍历每个布局,但似乎无法更新子图中的数字:

# Data Visualization
from plotly.subplots import make_subplots
import plotly.graph_objects as go

epoch_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
loss_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
val_loss_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
error_rate = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
val_error_rate = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

layout_list = []

loss_plots = [go.Scatter(x=epoch_list,
                         y=loss_list,
                         mode='lines',
                         name='Loss',
                         line=dict(width=4)),
              go.Scatter(x=epoch_list,
                         y=val_loss_list,
                         mode='lines',
                         name='Validation Loss',
                         line=dict(width=4))]
loss_layout = dict(font_color='black',
                   title_font_color='black',
                   title=dict(text='Loss Graph',
                              font_size=30),
                   xaxis_title=dict(text='Epochs',
                                    font_size=25),
                   yaxis_title=dict(text='Loss',
                                    font_size=25),
                   legend=dict(font_size=15))

loss_figure = go.Figure(data=loss_plots)
layout_list.append(loss_layout)

error_plots = [go.Scatter(x=epoch_list,
                          y=loss_list,
                          mode='lines',
                          name='Error Rate',
                          line=dict(width=4)),
               go.Scatter(x=epoch_list,
                          y=val_loss_list,
                          mode='lines',
                          name='Validation Error Rate',
                          line=dict(width=4))]
error_rate_layout = dict(font_color='black',
                         title_font_color='black',
                         title=dict(text='Error Rate Graph',
                                    font_size=30),
                         xaxis_title=dict(text='Epochs',
                                          font_size=25),
                         yaxis_title=dict(text='Error Rate',
                                          font_size=25),
                         legend=dict(font_size=15))

error_figure = go.Figure(data=error_plots)
layout_list.append(error_rate_layout)

metric_figure = make_subplots(
    rows=3, cols=2,
    specs=[[{}, {}],
           [{}, {}],
           [{}, {}]])

for t in loss_figure.data:
    metric_figure.append_trace(t, row=1, col=1)
for t in error_figure.data:
    metric_figure.append_trace(t, row=1, col=2)

for (figure, layout) in zip(metric_figure, layout_list):
    figure.update_layout(layout)

metric_figure.show()

似乎这样做也不起作用,因为布局没有转移,因为我只循环遍历轨迹:

loss_figure = go.Figure(data=loss_plots, layout=loss_layout)

标签: pythongraphplotlydata-scienceplotly-python

解决方案


  • 你可以使用 python dict合并技术

metric_figure.update_layout({**loss_layout, **error_rate_layout})

或者,如果布局在图中

metric_figure.update_layout({**error_figure.to_dict()["layout"],**error_ficture.to_dict()["layout"]})
  • 由于子图布局与单个图形有很大不同,因此这两种方法的用途都有限。与单个图形/布局相比,x 轴和 y 轴定义将不同,并且字典键重叠只能使用一个 - 例如标题

推荐阅读