python - 一种在 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)
解决方案
- 你可以使用 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 轴定义将不同,并且字典键重叠只能使用一个 - 例如标题
推荐阅读
- java - 未来
java的参数 - python - 我想通过python在Windows上以Android格式打开Firefox
- jquery - DevExtreme Charts - 如何使用 jQuery 确定图表类型
- python - 如何从数据框中获取计算出的标量
- reactjs - D3 + ReactJS 网络图在不断更新图后无限期挂起浏览器
- javascript - 在使用 Webpack 插件生成初始块后使用额外代码更新块
- node.js - 反应角色命令有问题... Node.js、Discord.js、Javascript
- google-bigquery - BigQuery -- 在公开共享项目中使用 UDF 的问题
- c - C API 确保用户资源保持分配的实用方法?
- macos - 带有 DPI 的 MacOS 图像文件缩放器