You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何抑制Plotly中update_layout等方法产生的中间绘图输出?

解决Plotly中间绘图输出的问题

问题原因

在Jupyter环境中,Plotly的update_*方法会返回修改后的Figure对象,如果这些语句作为单元格的执行结果被返回,Jupyter就会自动渲染显示该Figure,导致出现多个中间图。overwrite参数仅作用于布局更新,无法抑制这种自动输出。

解决方案

以下三种简单方法可解决该问题:

1. 链式调用所有更新方法

将所有update_traces、update_xaxes、update_yaxes、update_layout方法链式调用,整个流程只会返回一次最终的Figure对象,不会触发中间渲染:

import plotly

fig_Fahmy = plotly.express.line(
    data_melt3,
    x="Heat data",
    y="Flow data",
    color="variable",
    height=800,
    width=1300,
    title="Fahmy plot of heat flow ~ cumulative heat",
    labels={
        "Flow data": "Heat flow, W",
        "Heat data": "Cumulative heat, J",
        "variable": ""
    }
).update_traces(line=dict(width=2))\
.update_xaxes(
    linewidth=1, 
    linecolor='black', 
    mirror=True, 
    ticks='inside', 
    showline=True, 
    gridcolor="white", 
    zerolinecolor="darkgray", 
    title_font=dict(size=20), 
    tickfont=dict(size=15),
    ticklen=10,
    tickwidth=2
)\
.update_yaxes(
    linewidth=1, 
    linecolor='black', 
    mirror=True, 
    ticks='inside', 
    showline=True, 
    gridcolor="white", 
    zerolinecolor="darkgray", 
    title_font=dict(size=20), 
    tickfont=dict(size=15),
    ticklen=10,
    tickwidth=2
)\
.update_layout(
    plot_bgcolor="rgba(0, 0, 0, 0)",
    legend=dict(itemsizing='constant'),
    legend_font=dict(size=15),
    title_font=dict(size=25)
)

fig_Fahmy.show() if outputs == True else None

2. 在每个更新语句末尾加分号

在每个fig_Fahmy.update_*()语句末尾添加分号;,可抑制Jupyter自动输出该语句的返回值(即修改后的Figure):

import plotly

fig_Fahmy = plotly.express.line(
    data_melt3,
    x="Heat data",
    y="Flow data",
    color="variable",
    height=800,
    width=1300,
    title="Fahmy plot of heat flow ~ cumulative heat",
    labels={
        "Flow data": "Heat flow, W",
        "Heat data": "Cumulative heat, J",
        "variable": ""
    }
)

fig_Fahmy.update_traces(line=dict(width=2));  # 加分号抑制输出

fig_Fahmy.update_xaxes(
    linewidth=1, 
    linecolor='black', 
    mirror=True, 
    ticks='inside', 
    showline=True, 
    gridcolor="white", 
    zerolinecolor="darkgray", 
    title_font=dict(size=20), 
    tickfont=dict(size=15),
    ticklen=10,
    tickwidth=2
);  # 加分号抑制输出

fig_Fahmy.update_yaxes(
    linewidth=1, 
    linecolor='black', 
    mirror=True, 
    ticks='inside', 
    showline=True, 
    gridcolor="white", 
    zerolinecolor="darkgray", 
    title_font=dict(size=20), 
    tickfont=dict(size=15),
    ticklen=10,
    tickwidth=2
);  # 加分号抑制输出

fig_Fahmy.update_layout(
    plot_bgcolor="rgba(0, 0, 0, 0)",
    legend=dict(itemsizing='constant'),
    legend_font=dict(size=15),
    title_font=dict(size=25)
);  # 加分号抑制输出

fig_Fahmy.show() if outputs == True else None

3. 确保更新语句不是单元格的最后一行

在Jupyter Notebook中,将所有更新操作放在同一个代码单元格里,让fig_Fahmy.show()(或其他不返回Figure的语句)作为单元格的最后一行,中间的更新语句就不会被作为返回值触发渲染。

内容的提问来源于stack exchange,提问作者Václav Bočan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 18:51:31