我正在尝试创建两个单独的图形,我想使用 for 循环为每个图形添加绘图。
using Plots
gr()
Plots.theme(:juno, fmt = :png)
x = [1,2,3,4]
y = [8,7,6,4]
pt1 = plot()
pt2 = plot(reuse = false)
for i in 1:5
pt1 = plot!(x, i*y)
pt2 = plot!(x, y/i, reuse = false)
end
display(pt1)
display(pt2)
但我得到的是两个数字,其中包含 pt1
和 pt2
的所有图。
我尝试使用 push!
,但我发现的示例是制作 gif,这不是我想要做的。这似乎是最简单的方法,我一定是错过了一些明显的东西。
最佳答案
plot!
可以将绘图句柄作为第一个参数,因此它应该是 plot!(pt1, x, i*y)
。
这是完整的更正代码:
using Plots
gr()
Plots.theme(:juno, fmt = :png)
x = [1,2,3,4]
y = [8,7,6,4]
pt1 = plot()
pt2 = plot()
for i in 1:5
plot!(pt1, x, i*y)
plot!(pt2, x, y/i)
end
display(pt1)
display(pt2)
结果如下:
关于julia - 如何在 Julia 中使用 for 循环将绘图添加到单独的图形中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68136957/