Prevent plotting new figure

Hi

What can I do to prevent plotting new figure and my new data be plotted on previous figure? (I’m using Jupyter Notebook)

import plotly.graph_objects as go

fig = go.FigureWidget();

fig.add_trace(go.Bar(x=[1, 2, 3], y=[1, 3, 2]));

fig.show();

fig.update_traces(go.Bar(x=[4, 1, 6], y=[2, 5, 4]));

fig.show();

This is what I get, But I want only the first plot remain and get updated and prevent plotting new figure! (I’m using Jupyter Notebook)

Hi,

You can just remove the first fig.show().

Hi @jlfsjunior

But in this case, the first figure data won’t update and the initial data remains on the figure!

It is important for me that figure data can become update after Fig.show() command. imagine I have a loop and in each loop I have new data:

import plotly.graph_objects as go
import random as rnd

fig = go.FigureWidget();

fig.add_trace(go.Bar(x=[1, 2, 3], y=[1, 3, 2]));

fig.show()

for i in range(10):
      fig.update_traces(go.Bar(x=[rnd.random()*10, 1, 6], y=[2, 5, 4]));

Sorry, replied too fast… I don’t think the data can be updated via update_traces

You can try this instead:

fig = go.FigureWidget();
fig.add_trace(go.Bar(x=[1, 2, 3], y=[1, 3, 2]));

fig.data[0].x = [4, 1, 6]
fig.data[0].y = [2, 5, 4]

@jlfsjunior

Thanks for your contribution and guidance

I wrote this according your guidance:

import plotly.graph_objects as go
fig = go.FigureWidget();
fig.add_trace(go.Bar(x=[1, 2, 3], y=[1, 3, 2]));
fig.show();
fig.data[0].x = [4, 1, 6]
fig.data[0].y = [2, 5, 4]

But the figure didn’t update and the initial data remains. what is my mistake?

I am not super familiar with FigureWidget, but maybe fig.show() doesn’t allow for updates… The solution here doesn’t use it, for instance…

Another alternative is to go with a “full-fledged” widget, as described here.

Dude @jlfsjunior

You helped me. I found the answer by your great help: :+1: :pray:

import plotly.graph_objects as go
import random as rnd
fig = go.FigureWidget();
fig.add_trace(go.Bar(x=[1, 2, 3], y=[1, 3, 2]));
display(fig)
for i in range(10):
    fig.data[0].x = [rnd.random(), 1, 6]
    fig.data[0].y = [rnd.random(), 5, 4]
1 Like