Ben S.
Ben S.

Reputation: 3545

How to animate changing histogram in Plots.jl?

I'm working from the following example and failing miserably

# initialize the attractor
n = 1500
dt = 0.02
σ, ρ, β = 10., 28., 8/3
x, y, z = 1., 1., 1.

# initialize a 3D plot with 1 empty series
plt = path3d(1, xlim=(-25,25), ylim=(-25,25), zlim=(0,50),
                xlab = "x", ylab = "y", zlab = "z",
                title = "Lorenz Attractor", marker = 1)

# build an animated gif, saving every 10th frame
@gif for i=1:n
    dx = σ*(y - x)     ; x += dt * dx
    dy = x*(ρ - z) - y ; y += dt * dy
    dz = x*y - β*z     ; z += dt * dz
    push!(plt, x, y, z)
end every 10

I have simulations in which I want to calculate and plot a histogram every so often, and then animate all those histograms. This is what I have so far as a toy example:

using Distributions
using Plots
Plots.gr()

p = rand(Normal(0,1), 10000)
myplot = Plots. histogram(p)
savefig("initial.png")

anim = @animate for i=1:10
    p = rand(Normal(0,1), 10000)
    push!(myplot, Plots.histogram(p))
end

gif(anim, "mygif.gif", fps = 1)

As you can probably tell, I don't really get how this @animate thing works and am just guessing at the syntax here. I know that I could save a bunch of PNGs and later animate, but I want to try it this way.

Upvotes: 4

Views: 1352

Answers (1)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

anim = @animate for i=1:10
    p = rand(Normal(0,1), 10000)
    histogram(p)
end

gif(anim, "mygif.gif", fps = 1)

should work.

Upvotes: 5

Related Questions