Search code examples
pythonplotplotlyplotly-express

How to suppress plotting of plots from update_layout in plotly?


I'm plotting using plotly and plotly express a line graph, which is updated using fig.update_figure. However every updated parameter in update_figure results in the plot being plotted as a graphical output, so I end up having four intermediate plots and final one. How can I suppress outputs from fig.update_figue? I thought the overwrite parameter would help but obviously not. Here's how the outputs look like

enter image description here

Here is the actual code:

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(
    overwrite=True,
    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

It's probably something obvious but I'm kinda new in this.


Solution

  • To supress multiple outputs just overwrite your figure variable and keep the fig_Fahmy.show() statement at the end of your script.

    For example your first update line:

    fig_Fahmy = fig_Fahmy.update_traces(line=dict(width=2))