gpt4 book ai didi

plot - Julia 绘图 : delete and modify existing lines

转载 作者:行者123 更新时间:2023-12-01 09:12:24 24 4
gpt4 key购买 nike

两个问题合二为一:鉴于在 Julia 中绘制的一条线,我如何才能

  • 从情节和图例中删除它(不清除整个情节)
  • 更改其属性(如颜色、厚度、不透明度)

  • 作为下面代码中的一个具体示例,我如何 1. 删除以前的回归线或 2. 将它们的不透明度更改为 0.1?
    using Plots; gr()

    f = x->.3x+.2
    g = x->f(x)+.2*randn()

    x = rand(2)
    y = g.(x)
    plt = scatter(x,y,c=:orange)
    plot!(0:.1:1, f, ylim=(0,1), c=:green, alpha=.3, linewidth=10)

    anim = Animation()
    for i=1:200
    r = rand()
    x_new, y_new = r, g(r)
    push!(plt, x_new, y_new)
    push!(x, x_new)
    push!(y, y_new)
    A = hcat(fill(1., size(x)), x)
    coefs = A\y
    plot!(0:.1:1, x->coefs[2]*x+coefs[1], c=:blue) # plot new regression line
    # 1. delete previous line
    # 2. set alpha of previous line to .1
    frame(anim)
    end
    gif(anim, "regression.gif", fps=5)

    我试过删除组合,弹出!并删除但没有成功。
    可以在此处找到 Python 中的相关问题: How to remove lines in a Matplotlib plot

    regression

    最佳答案

    这是一个有趣且说明性的示例,说明如何使用 pop!()使用 Makie 在 Julia 中撤消绘图。请注意,您将看到这以与绘制所有内容相反的顺序返回(想想,就像从堆栈中添加和删除),所以 deleteat!(scene.plots, ind)仍然需要删除特定索引处的图。


    using Makie

    x = range(0, stop = 2pi, length = 80)
    f1(x) = sin.(x)
    f2(x) = exp.(-x) .* cos.(2pi*x)
    y1 = f1(x)
    y2 = f2(x)

    scene = lines(x, y1, color = :blue)
    scatter!(scene, x, y1, color = :red, markersize = 0.1)

    lines!(scene, x, y2, color = :black)
    scatter!(scene, x, y2, color = :green, marker = :utriangle, markersize = 0.1)

    display(scene)

    Inital plot
    sleep(10)
    pop!(scene.plots)
    display(scene)

    2nd image
    sleep(10)
    pop!(scene.plots)
    display(scene)


    3rd picture

    您可以看到上面的图片,这些图片显示了如何使用 pop() 逐步撤消情节。 .关于 sleep()的关键思想是如果我们不使用它(并且您可以通过运行删除它的代码自行测试),由于渲染时间,屏幕上显示的第一个也是唯一的图像将是上面的最终图像。

    您可以查看是否运行此窗口渲染的代码,然后休眠 10 秒(以便给它时间渲染),然后使用 pop!()退回情节。

    Docs for sleep()

    关于plot - Julia 绘图 : delete and modify existing lines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57829961/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com