# Can you update a plot in Julia?

**URL:** <https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448>\
**Category:** New to Julia\
**Tags:** visualization\
**Created:** [March 28, 2019, 9:56am UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448 "2019-03-28T09:56:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [March 28, 2019, 9:56am UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448/1 "2019-03-28T09:56:01Z")

</div>

Can you update an existing plot in Julia? For example, if I have a plot with two lines (series), can I change the data or color of one of the lines? For example:

```julia
using Plots

gr()

x = range(0, 2π, length=1000)
w = plot(size=(1000, 700))
plot!(x, sin.(2x)) # First line
plot!(x, sin.(3x)) # Second line

```

Can I somehow change/update the second series? Something like this, perhaps:  
`plot!(w.series_list[2], x, sin.(3x)+0.1*randn(1000))`

I guess this should be really simple, but I have not found it anywhere.

---

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [March 29, 2019, 10:11am UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448/2 "2019-03-29T10:11:23Z")

</div>

Good question, I never seen an example of that.  
I don’t know if you really need that feature, I do all wrong plots until reach my desired result and its not a big problem - but I’m speaking for myself.

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [March 29, 2019, 12:29pm UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448/3 "2019-03-29T12:29:25Z")

</div>

The Plots.jl documentation includes an example of an animation:

[http://docs.juliaplots.org/latest/examples/gr/#functions-adding-data-and-animations](http://docs.juliaplots.org/latest/examples/gr/#functions-adding-data-and-animations)

There are more details in the documentation, but I included a copy of the sample code here:

```julia
p = plot([sin, cos], zeros(0), leg=false)
anim = Animation()
for x = range(0, stop=10π, length=100)
    push!(p, x, Float64[sin(x), cos(x)])
    frame(anim)
end

```

I myself posted an example on how to do this directly with the InspectDR package (for faster refresh rates):

> [@Realtime plotting](https://discourse.julialang.org/t/realtime-plotting/15638/6):
>
> I was wondering how InspectDR would handle this myself… so I made a small test: using InspectDR using Colors function getmeasdata(t, φ) sigA = sin.(t .+ φ) sigB = cos.(t .+ φ) return (sigA, sigB) end #Build general structure of animation plot function buildanimplot() NPOINTS = 1000 NCYCLES = 2 #To display RED = RGB24(1, 0, 0) GREEN = RGB24(0, 1, 0) BLUE = RGB24(0, 0, 1) #collect: InspectDR does not take AbstractArray: t = collect(range(0, stop=NCYCLES\*2pi, length=NPOINTS)) (sigA, …

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [March 29, 2019, 9:16pm UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448/4 "2019-03-29T21:16:53Z")

</div>

The animation is not quite what I wanted, or then I don’t understand it. Can you change one of the series with the animation?

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [March 29, 2019, 11:29pm UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448/5 "2019-03-29T23:29:54Z")

</div>

Sorry about that. I just tried out the code. I am pretty certain it _ **used** _ to work at some point before Julia v0.7, but the snippet uses deprecated syntax.

Sadly, I could not get it to work either.

### Alternative

Instead of “trying” to add a third series as you were doing with the following line:  
`plot!(w.series_list[2], x, sin.(3x)+0.1*randn(1000))`

(Sorry, with the Plots.jl interface, `plot!()` means “modify the plot as a whole” (ie append to the plot)… it does not mean to modify the given dataset)

you could hack into the data structures in order to modify the dataset:

```julia-auto
display(w) #Show original plot
sleep(1) #Pause for the user to see changes
w.series_list[2].plotattributes[:y] = sin.(3x)+0.1*randn(1000)
display(w) #Display plot agiain with newly modified dataset

```

But I just don’t know if this is considered an accepted/supported method of updating plot data.

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [March 29, 2019, 11:31pm UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448/6 "2019-03-29T23:31:05Z")

</div>

This kind of question is the sort of thing you could ask on the Plots.jl gitter channel:

[https://gitter.im/tbreloff/Plots.jl](https://gitter.im/tbreloff/Plots.jl)

(you will probably get a quicker answer there)

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [March 29, 2019, 11:40pm UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448/7 "2019-03-29T23:40:57Z")

</div>

If you don’t get thing to work with Plots.jl, you could try using InspectDR with the link above.

Note that the example only _ **looks** _ complicated, because

- I control the plot appearance a bit more
  - I even declare colors RED, GREEN, BLUE, and the timing parameters explicitly.

- The API is not as succinct as Plots.jl (meant for use in scripts; not on-the-fly plotting)
- I placed some of the code in functions:
  - `getmeasdata()`: convenient method for generating data given the time step
  - `buildanimplot()`: sets up the plot.
  - `testanimplot()`: Does the actual animation by replacing series data after a given time step.

---

<div class="post-metadata">

**Author:** ![williamjsdavis](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@williamjsdavis](https://discourse.julialang.org/u/williamjsdavis)\
**Post date:** [April 4, 2020, 12:25am UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448/8 "2020-04-04T00:25:46Z")

</div>

I made a package called [SmoothLivePlot.jl](https://github.com/williamjsdavis/SmoothLivePlot.jl), that can do this. It’s based on [ckneale](https://discourse.julialang.org/u/ckneale)’s solution [in this post](https://discourse.julialang.org/t/current-state-of-live-plots-in-atom-juno/30379/7). Hope it can be useful for other people too.

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [April 4, 2020, 2:01pm UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448/9 "2020-04-04T14:01:42Z")

</div>

Awesome! William have you seen the animation functions in Plots.jl?  
Either way I like your solution :). What I really want out of anything in the whole wide world is Plots.jl to become “clickable” do you think that’s possible with Observables.jl?

---

<div class="post-metadata">

**Author:** ![williamjsdavis](https://avatars.discourse-cdn.com/v4/letter/w/9fc348/32.png) [@williamjsdavis](https://discourse.julialang.org/u/williamjsdavis)\
**Post date:** [April 5, 2020, 5:50pm UTC](https://discourse.julialang.org/t/can-you-update-a-plot-in-julia/22448/10 "2020-04-05T17:50:40Z")

</div>

Thank you! Yes I have seen the animation features of plots, they’re very useful. The package I posted above is more of a quick visualisation tool, rather than making something of presentation quality that is saved. I’ll have a think about your point about clickable elements in Observables.jl, that would certainly be useful!
