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.
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.
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!