I am trying get familiar with packages in Julia like Interpolations.jl
and Plots.jl
. The contour plot in Julia using the GR backend is very simple as shown in the link:
https://docs.juliaplots.org/latest/examples/gr/#contours
And to carry out interpolation using Interpolations.jl
:
https://github.com/JuliaMath/Interpolations.jl/blob/master/doc/Interpolations.jl.ipynb
I tried to make a contour plot using an interpolated function as following:
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)
However this gives a plot without any contour curves on it, with a message saying "Arrays have incorrect length or dimension.". But the contour
function seems to accept, as in the link above, arbitrary x, y arrays and a function of x, y, and returns a contour plot. This should not give rise to any dimension problems. What is wrong with the code?
[Edit]
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)
The result:
The interpolation seems to work fine but the result from the contour plot doesn't seem to convey the same message.