Julia Plotting:删除和修改现有行

unt*_*gam 6 plot setattribute julia

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

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

作为下面代码中的一个具体示例,我如何 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)
Run Code Online (Sandbox Code Playgroud)

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

回归

小智 5

我不得不说,我不知道完成它们的正式方法是什么。

有一种作弊方法。

plt.series_list存储所有图(线、散点图...)。
如果图中有 200 条线,则为length(plt.series_list)200。

plt.series_list[1].plotattributes返回包含第一行属性的字典(或散点图,取决于顺序)。

其中一个属性是:linealpha,我们可以用它来修改线条的透明度或让它消失。

# your code ...

plot!(0:.1:1, x->coefs[2]*x+coefs[1], c=:blue)  # plot new regression line

# modify the alpha value of the previous line
if i > 1
    plt.series_list[end-1][:linealpha] = 0.1
end

# make the previous line invisible
if i > 2
    plt.series_list[end-2][:linealpha] = 0.0
end

frame(anim)
# your code ...
Run Code Online (Sandbox Code Playgroud)


log*_*ick 5

这是一个有趣且说明性的示例,说明如何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)
Run Code Online (Sandbox Code Playgroud)

初始情节

sleep(10)
pop!(scene.plots)
display(scene)
Run Code Online (Sandbox Code Playgroud)

第二张图片

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

Run Code Online (Sandbox Code Playgroud)

第三张图片

您可以看到上面的图像显示了如何使用 逐步撤消绘图pop()。关键的想法sleep()是,如果我们没有使用它(您可以通过运行删除它的代码来自行测试它),屏幕上显示的第一个也是唯一的图像将是上面的最终图像,因为渲染时间。

您可以看到,如果运行此代码,窗口会渲染,然后休眠 10 秒(以便有时间渲染),然后用于pop!()后退查看绘图。

睡眠文档()