# Prevent plotting new figure

**URL:** https://community.plotly.com/t/prevent-plotting-new-figure/61596
**Category:** 📊 Plotly Python
**Created:** [March 3, 2022, 7:06am UTC](https://community.plotly.com/t/prevent-plotting-new-figure/61596 "2022-03-03T07:06:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Bijan](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/bijan/32/22061_2.png) [@Bijan](https://community.plotly.com/u/Bijan)
#### Post date: [March 3, 2022, 7:06am UTC](https://community.plotly.com/t/prevent-plotting-new-figure/61596/1 "2022-03-03T07:06:04Z")

</div>

Hi

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

```auto
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();

```

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/c/e/ceeae7083dde6f9a998ccb0a09945f989b34666c.png)

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)

---

<div class="post-metadata">

### Author: ![jlfsjunior](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jlfsjunior/32/16061_2.png) [@jlfsjunior](https://community.plotly.com/u/jlfsjunior)
#### Post date: [March 3, 2022, 7:10am UTC](https://community.plotly.com/t/prevent-plotting-new-figure/61596/2 "2022-03-03T07:10:11Z")

</div>

Hi,

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

---

<div class="post-metadata">

### Author: ![Bijan](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/bijan/32/22061_2.png) [@Bijan](https://community.plotly.com/u/Bijan)
#### Post date: [March 3, 2022, 7:13am UTC](https://community.plotly.com/t/prevent-plotting-new-figure/61596/3 "2022-03-03T07:13:24Z")

</div>

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:

```auto
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]));

```

---

<div class="post-metadata">

### Author: ![jlfsjunior](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jlfsjunior/32/16061_2.png) [@jlfsjunior](https://community.plotly.com/u/jlfsjunior)
#### Post date: [March 3, 2022, 7:18am UTC](https://community.plotly.com/t/prevent-plotting-new-figure/61596/4 "2022-03-03T07:18:38Z")

</div>

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

You can try this instead:

```nohighlight
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]

```

---

<div class="post-metadata">

### Author: ![Bijan](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/bijan/32/22061_2.png) [@Bijan](https://community.plotly.com/u/Bijan)
#### Post date: [March 3, 2022, 7:23am UTC](https://community.plotly.com/t/prevent-plotting-new-figure/61596/5 "2022-03-03T07:23:09Z")

</div>

@jlfsjunior

Thanks for your contribution and guidance

I wrote this according your guidance:

```auto
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?

---

<div class="post-metadata">

### Author: ![jlfsjunior](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jlfsjunior/32/16061_2.png) [@jlfsjunior](https://community.plotly.com/u/jlfsjunior)
#### Post date: [March 3, 2022, 7:39am UTC](https://community.plotly.com/t/prevent-plotting-new-figure/61596/6 "2022-03-03T07:39:46Z")

</div>

I am not super familiar with FigureWidget, but maybe `fig.show()` doesn’t allow for updates… The solution [here](https://community.plotly.com/t/unable-to-update-data-keep-creating-new-one-instead/29696/2) doesn’t use it, for instance…

Another alternative is to go with a “full-fledged” widget, as described [here](https://plotly.com/python/v3/figurewidget-app/).

---

<div class="post-metadata">

### Author: ![Bijan](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/bijan/32/22061_2.png) [@Bijan](https://community.plotly.com/u/Bijan)
#### Post date: [March 3, 2022, 7:46am UTC](https://community.plotly.com/t/prevent-plotting-new-figure/61596/7 "2022-03-03T07:46:20Z")

</div>

Dude @jlfsjunior

You helped me. I found the answer by your great help: 👍 🙏

```auto
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]

```
