Julia Plotting:push!() 添加到错误的行

unt*_*gam 0 plot animation append julia

我想push!()在 for 循环中使用来继续两条单独的绘图线。考虑以下示例:

using Plots; gr()

f = x->x*x
g = x->f(x)+.2*randn()
p = plot(-1:.1:0, f, ylim=(-1,2), c=:blue)
s = scatter!(-1:.1:0, g, c=:red)

anim = Animation()
for i=1:10
    x = i/10
    push!(p, x, f(x))
    push!(s, x, g(x)) # without this, f gets continued as expected
    frame(anim)
end
gif(anim, "test.gif", fps=2)
Run Code Online (Sandbox Code Playgroud)

为什么push!(p, ...)push!(s,...)两者都继续蓝线?如何将分散的数据附加到s

将数据添加到错误行

我知道此链接的第二个图通过同时绘制和推动两条线获得了类似的结果,但该解决方案并不总是实用的,尤其是在更复杂的图中。

小智 5

在您的代码中,ps是相同的对象。
这意味着p == s将返回true

数据将存储在p.series_list.
您可以将 y 轴数据p.series_list[1][:y]用于线图p.series_list[2][:y]和散点图。

现在只需稍微修改原始代码即可!
首先,仅删除s和使用p
其次,在 push!() 函数中,给出了第二个参数来指示我们要追加新数据的数据索引。

f = x->x*x
g = x->f(x)+.2*randn()
p = plot(-1:.1:0, f, ylim=(-1,2), c=:blue)
scatter!(-1:.1:0, g, c=:red)

anim = Animation()
for i=1:10
    x = i/10
    push!(p, 1, x, f(x))
    push!(p, 2, x, g(x))
    frame(anim)
end
gif(anim, "test.gif", fps=2)
Run Code Online (Sandbox Code Playgroud)