在 Julia 中使用带有插值函数的 gr() 绘制等高线图
Contour plot using gr() with an interpolated function in Julia
我正在尝试熟悉 Julia 中的包,例如 Interpolations.jl
和 Plots.jl
。使用 GR 后端的 Julia 等高线图非常简单,如 link 所示:
https://docs.juliaplots.org/latest/examples/gr/#contours
并使用 Interpolations.jl
进行插值:
https://github.com/JuliaMath/Interpolations.jl/blob/master/doc/Interpolations.jl.ipynb
我尝试使用如下插值函数绘制等高线图:
using Interpolations
using Plots
gr()
xs = 1:5
ys = 1:8
g = Float64[(3x + y ^ 2) * abs(sin(x) + cos(y)) for x in xs, y in ys]
gitp_quad2d = interpolate(g, BSpline(Quadratic(Line(OnCell()))))
xc = 1:0.1:5
yc = 1:0.1:5
p1 = contour(xc, yc, gitp_quad2d, fill=true)
plot(p1)
然而,这给出了一个没有任何等高线曲线的图,并带有一条消息说 "Arrays have incorrect length or dimension."。但是 contour
函数似乎可以接受任意 x、y 数组和 x、y 的函数,以及 returns 等高线图,如上面的 link 所示。这不应引起任何尺寸问题。代码有什么问题?
[编辑]
using Interpolations
using Plots
gr()
xs = 1:0.5:5
ys = 1:0.5:8
g = Float64[(3x + y ^ 2) for x in xs, y in ys]
f(x, y) = (3x + y ^ 2)
g_int = interpolate(g, BSpline(Quadratic(Line(OnCell()))))
gs_int = scale(g_int, xs, ys)
xc = 1:0.1:5
yc = 1:0.1:5
println("gs_int(3.2, 3.2) = ", gs_int(3.2, 3.2))
println("f(3.2, 3.2) = ", f(3.2, 3.2))
p1 = contour(xs, ys, gs_int(xs, ys), fill=true)
p2 = contour(xc, yc, f, fill=true)
plot(p1, p2)
结果:
插值似乎工作正常,但等高线图的结果似乎没有传达相同的信息。
您需要指定希望插值发生的点 - 否则您只会获得插值对象的输入分辨率,这与新的 xc 和 yc 不同(尝试 size(gitp_quad2d)
. Plots 中没有内置的方法可以在 x 和 y 输入上自动执行此操作。
尝试
contourf(xc, yc, gitp_quad2d[xc, yc])
编辑:已更新以反映问题的更新
在你的图上,等高线看起来很奇怪的原因是你的插值矩阵相对于 x 和 y 变量进行了转置。 contour/heatmaps 期望的转置一直是绘图中的讨论(它应该与矩阵相同,作为普通绘图还是作为图像? - 请参阅此问题以获得良好的讨论 https://github.com/JuliaPlots/Makie.jl/issues/205)。无论如何,将其移回会有帮助(p1 = contourf(xs, ys, gs_int(xs, ys)')
或 p1 = contourf(xs, ys, gs_int(xs, ys), transpose = true)
我正在尝试熟悉 Julia 中的包,例如 Interpolations.jl
和 Plots.jl
。使用 GR 后端的 Julia 等高线图非常简单,如 link 所示:
https://docs.juliaplots.org/latest/examples/gr/#contours
并使用 Interpolations.jl
进行插值:
https://github.com/JuliaMath/Interpolations.jl/blob/master/doc/Interpolations.jl.ipynb
我尝试使用如下插值函数绘制等高线图:
using Interpolations
using Plots
gr()
xs = 1:5
ys = 1:8
g = Float64[(3x + y ^ 2) * abs(sin(x) + cos(y)) for x in xs, y in ys]
gitp_quad2d = interpolate(g, BSpline(Quadratic(Line(OnCell()))))
xc = 1:0.1:5
yc = 1:0.1:5
p1 = contour(xc, yc, gitp_quad2d, fill=true)
plot(p1)
然而,这给出了一个没有任何等高线曲线的图,并带有一条消息说 "Arrays have incorrect length or dimension."。但是 contour
函数似乎可以接受任意 x、y 数组和 x、y 的函数,以及 returns 等高线图,如上面的 link 所示。这不应引起任何尺寸问题。代码有什么问题?
[编辑]
using Interpolations
using Plots
gr()
xs = 1:0.5:5
ys = 1:0.5:8
g = Float64[(3x + y ^ 2) for x in xs, y in ys]
f(x, y) = (3x + y ^ 2)
g_int = interpolate(g, BSpline(Quadratic(Line(OnCell()))))
gs_int = scale(g_int, xs, ys)
xc = 1:0.1:5
yc = 1:0.1:5
println("gs_int(3.2, 3.2) = ", gs_int(3.2, 3.2))
println("f(3.2, 3.2) = ", f(3.2, 3.2))
p1 = contour(xs, ys, gs_int(xs, ys), fill=true)
p2 = contour(xc, yc, f, fill=true)
plot(p1, p2)
结果:
插值似乎工作正常,但等高线图的结果似乎没有传达相同的信息。
您需要指定希望插值发生的点 - 否则您只会获得插值对象的输入分辨率,这与新的 xc 和 yc 不同(尝试 size(gitp_quad2d)
. Plots 中没有内置的方法可以在 x 和 y 输入上自动执行此操作。
尝试
contourf(xc, yc, gitp_quad2d[xc, yc])
编辑:已更新以反映问题的更新
在你的图上,等高线看起来很奇怪的原因是你的插值矩阵相对于 x 和 y 变量进行了转置。 contour/heatmaps 期望的转置一直是绘图中的讨论(它应该与矩阵相同,作为普通绘图还是作为图像? - 请参阅此问题以获得良好的讨论 https://github.com/JuliaPlots/Makie.jl/issues/205)。无论如何,将其移回会有帮助(p1 = contourf(xs, ys, gs_int(xs, ys)')
或 p1 = contourf(xs, ys, gs_int(xs, ys), transpose = true)