plot - Julia 绘图 : delete and modify existing lines

标签 plot julia setattribute

两个问题合二为一:鉴于在 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/

    相关文章:

    julia - Julia 中推荐的数据结构以实现高效追加

    javascript - 如何更改javascript中动态创建的按钮的值

    python - 跳过 seaborn facetgrid 中的空面以​​进行注释

    python - 实时绘制图形搜索

    multidimensional-array - Julia - 使用 mvNormal 生成具有给定均值和协方差矩阵的多元高斯样本

    arrays - Julia在数组中选择最长数组的最有效方法?

    javascript - 选中/取消选中复选框取决于数据来自数据库

    javascript - 从幻灯片中的图像获取图像 src

    r - 绘制最佳拟合线 R

    r - 如何在绘图 (ggplot2) 的文本注释中放置 +/- 加减运算符?