Can you update a plot in Julia?

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:

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.

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.

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

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:

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):

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?

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:

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.

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

https://gitter.im/tbreloff/Plots.jl

(you will probably get a quicker answer there)

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.

I made a package called SmoothLivePlot.jl, that can do this. It’s based on ckneale’s solution in this post. Hope it can be useful for other people too.

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?

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!