Julia Plotting:删除和修改现有线条

Julia Plotting: delete and modify existing lines

两个问题合二为一:给定一条在 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)

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

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

有作弊方法。

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

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

其中一个属性是: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 ...

您不能使用 Plots 包来做到这一点。即使是 Pei Huang 的答案中的 "cheating" 方法,最终也会重绘整个框架。 不过,您可以使用 Makie 做到这一点——事实上,交互式更改绘图的能力是创建该包的原因之一(此处第 1 点 http://makie.juliaplots.org/dev/why-makie.html) 不确定 Julia 的其他流行绘图包。

这是一个有趣的说明性示例,说明如何使用 pop!() 在 Julia 中使用 Makie 撤消绘图。请注意,您会看到它以与绘制所有内容相反的顺序返回(想想,就像从堆栈中添加和删除),因此仍然需要 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)

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

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

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

你可以看看你是否 运行 这段 window 渲染的代码然后休眠 10 秒(为了给它时间渲染)然后使用 pop!() 来退一步看情节。

Docs for sleep()