# Observable plot re-rendering when data changes

**URL:** <https://talk.observablehq.com/t/observable-plot-re-rendering-when-data-changes/8398>\
**Category:** Help\
**Created:** [October 19, 2023, 5:51pm UTC](https://talk.observablehq.com/t/observable-plot-re-rendering-when-data-changes/8398 "2023-10-19T17:51:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfalbel](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/dfalbel/32/8121_2.png) [@dfalbel](https://talk.observablehq.com/u/dfalbel)\
**Post date:** [October 19, 2023, 5:51pm UTC](https://talk.observablehq.com/t/observable-plot-re-rendering-when-data-changes/8398/1 "2023-10-19T17:51:46Z")

</div>

Hello all 🙂

I have some data that is updated via a generator, and I’m using a mutable data cell that stores the full dataset that I’m later ploting using the Plot.

I’m trying to understand why the Plot is not updated when the `data` cell changes, I’d expect it to be modified, since the Plot cell depends on that data.

Here’s a minimal example notebook:

> **[Untitled](https://observablehq.com/d/f441bb5596deabfe)**

I know how to solve this using another generator for the plot (like below), I’m mostly trying to understand why the above is wrong.

```auto
{
  do {
    yield Plot.plot({
      marks: [
        Plot.lineY(
         data, 
          {y: "hello"},
        )
      ]
    })
    await Promises.delay(100)
  } while(true)
}

```

Thank you very much!

---

<div class="post-metadata">

**Author:** ![mythmon](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mythmon/32/8504_2.png) [@mythmon](https://talk.observablehq.com/u/mythmon)\
**Post date:** [October 19, 2023, 6:09pm UTC](https://talk.observablehq.com/t/observable-plot-re-rendering-when-data-changes/8398/2 "2023-10-19T18:09:31Z")

</div>

Mutable cells don’t trigger re-renders when their value changes; they trigger re-renders when they are _assigned_.

You would need to change this cell

```js
mutable data.push({hello: a})

```

to one that assigns to `data`. You could do this for example:

```js
mutable data = [...mutable data, {hello: a}]

```

However, I would suggest you avoid using mutable if you can avoid it. Instead, in this case, I would do something like this:

```js
data = {
  let values = [];
  while (true) {
    yield values;
    values.push({hello: Math.random()});
    await Promises.delay(1000);
  }
}

```

---

<div class="post-metadata">

**Author:** ![eagereyes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/eagereyes/32/6255_2.png) [@eagereyes](https://talk.observablehq.com/u/eagereyes)\
**Post date:** [October 19, 2023, 6:15pm UTC](https://talk.observablehq.com/t/observable-plot-re-rendering-when-data-changes/8398/3 "2023-10-19T18:15:38Z")

</div>

I just made a fork of your notebook that works a bit like mythmon describes: [Untitled / Robert Kosara | Observable](https://observablehq.com/d/3dc0a5d47c000549)

You don’t need mutable for a value to change. Can you describe what you’re trying to accomplish?

---

<div class="post-metadata">

**Author:** ![dfalbel](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/dfalbel/32/8121_2.png) [@dfalbel](https://talk.observablehq.com/u/dfalbel)\
**Post date:** [October 20, 2023, 1:45pm UTC](https://talk.observablehq.com/t/observable-plot-re-rendering-when-data-changes/8398/4 "2023-10-20T13:45:01Z")

</div>

Thank you both! This is really nice!  
@eagereyes I think it’s mostly lack of experience with JavaScript 😛

---

<div class="post-metadata">

**Author:** ![rusosnith](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/rusosnith/32/6367_2.png) [@rusosnith](https://talk.observablehq.com/u/rusosnith)\
**Post date:** [October 20, 2023, 10:32pm UTC](https://talk.observablehq.com/t/observable-plot-re-rendering-when-data-changes/8398/5 "2023-10-20T22:32:17Z")

</div>

It would be amazing if you could re post here the " fixed" notebook (or the same one you used as simplified example) with the corrected version for the posterity (or chatgp?)
