Reputation: 45
I saw in Animating subplots using Plots.jl efficiently that there were solutions to make the animation go faster, using the same methods as function 3 and 4 of the answer
...
p[1][1][:z] = data
p[2][1][:z] = data
...
but as here I do not want the shared z-axis, I do not know how to adapt the code. I searched for solutions in the documentation of subplots, but found nothing that worked.
The code that works but which is very slow is
p1 = plot3d(1, xlim = (-5, 5), ylim = (-5, 5), zlim = (-5, 5), markersize = 2, seriestype = :scatter, legend = false, aspect_ratio = 1)
p2 = histogram(randn(1), bins = 50, xlims = (0,7), ylims = (0, 20), legend = false)
p = plot(p1, p2, layout = (1,2), title = "Cluster Simulation")
anim = @animate for iter=1:Ncalc
step!(sim)
scatter!(p1, sim.pos[:,1], sim.pos[:,2], sim.pos[:,3], xticks = false, yticks = false, zticks = false)
histogram!(p2, sim.rad[:], xticks = false, yticks = false)
next!(prog)
end every Int(Ncalc/Nframes)
Here is the code which does not work:
p1 = plot3d(1, xlim = (-5, 5), ylim = (-5, 5), zlim = (-5, 5), markersize = 2, seriestype = :scatter, legend = false, aspect_ratio = 1)
p2 = histogram(randn(1), bins = 50, xlims = (0,7), ylims = (0, 20), legend = false)
p = plot(p1, p2, layout = (1,2), title = "Cluster Simulation")
anim = @animate for iter=1:Ncalc
step!(sim)
p[1] = sim.pos[:,1], sim.pos[:,2], sim.pos[:,3]
p[2] = sim.rad[:]
next!(prog)
end every Int(Ncalc/Nframes)
The error I get is the following:
ERROR: MethodError: no method matching setindex!(::Plots.Plot{Plots.GRBackend}, ::Array{Float64,1}, ::Int64)
Where did I miss something?
Thanks in advance
Upvotes: 2
Views: 244
Reputation: 3015
The error is telling you that you can't set the value of p[1]
to a vector of Float64
s. That's because p[1]
is a plot object, not just the (x,y,z)
data tuple. You need to assign the data to the :x
, :y
, and :z
fields of the first (only) data series. I can't test this without your data, but I think you want to do
p[1][1][:x] = sim.pos[:,1] # p[1][1] == p1[1] is the series
p[1][1][:y] = sim.pos[:,2] # so p[1][1][:x] is the x data, and so on...
p[1][1][:z] = sim.pos[:,3]
for p1
.
For p2
, you'd have to do more work to get the histogram's data and update the bars "manually" (via StatsBase.jl I think). Not sure how to best do that, but maybe the other part speeds up your animation creation enough already?
Upvotes: 1