julia - 如何在 Julia 中使用 for 循环将绘图添加到单独的图形中

标签 julia ijulia-notebook julia-plots

我正在尝试创建两个单独的图形,我想使用 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)

我希望得到两个数字,就好像我单独做它们一样: Test of pt1 Test of pt2

但我得到的是两个数字,其中包含 pt1pt2 的所有图。 Actual results

我尝试使用 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)

结果如下:

enter image description here

enter image description here

关于julia - 如何在 Julia 中使用 for 循环将绘图添加到单独的图形中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68136957/

相关文章:

julia - 预编译 StatsPlots.jl 失败

julia - Julia 散点图中的大小和颜色

julia - 在包代码上使用 Infiltrator.jl 而无需将 Infitrator 添加到 Project.toml

matrix - 在 Julia 中将多个参数作为一个参数传递

julia - (x :y) operator in Julia

jupyter-notebook - 如何共享 Jupyter Notebook?

julia - 数组的长度或维度不正确。 Julia 中的等高线图

julia - 如何在 Julia 中按值从数组中删除元素?

julia - 如何在 .juliarc 文件夹中添加文件夹以在 Julia 中运行程序?