Can you update a plot in Julia?

visualization
#1

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.

0 Likes

#2

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.

0 Likes

#3

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

0 Likes

#4

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?

0 Likes

#5

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.

0 Likes

#6

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

(you will probably get a quicker answer there)

1 Like

#7

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.
0 Likes