【问题标题】:Julia Plotting: push!() adds to wrong lineJulia Plotting: push!() 添加到错误的行
【发布时间】:2019-09-06 18:53:02
【问题描述】:

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

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)

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

我知道link 的第二个图通过同时绘制和推送两条线实现了类似的结果,但这种解决方案并不总是实用的,尤其是在更复杂的图中。

【问题讨论】:

    标签: animation plot append julia


    【解决方案1】:

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

    数据将存储在p.series_list
    您可以在p.series_list[1][:y] 中获取您的 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)
    

    【讨论】:

    • 好的,谢谢,这行得通!我不知道series_list。有没有办法获取特定行的句柄并对其进行修改?索引解决方案似乎有点容易出错:想象一下交换图和散点图,或者在两者之间插入第三个图 - 硬编码的索引总是需要更新。某种变量会更方便。想法?
    • 抱歉,我不知道最佳答案是什么。在我看来,(1)您可以使用字典在您的情节和索引之间创建链接。 (2) 使用数组存储数据,循环中每一轮都绘制一个新图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多