3
votes

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: a

The interpolation seems to work fine but the result from the contour plot doesn't seem to convey the same message.

1

1 Answers

4
votes

You need to specify at what points you want the interpolation to happen - otherwise you will just get the input resolution of the interpolated object, which is different from that of the new xc and yc (try size(gitp_quad2d). There isn't a recipe built into Plots for doing that automatically on the x and y inputs. Try

contourf(xc, yc, gitp_quad2d[xc, yc])

EDIT: updated to reflect update of question

On your plot, the reason you have the contour looking strangely is that your interpolated matrix is transposed relative to the x and y variables. The transposition expected of contour/heatmaps is always a discussion in plotting (should it be the same as matrices, as a normal plot or as an image? - see this issue for a good discussion https://github.com/JuliaPlots/Makie.jl/issues/205). Anyway, transposing it back will help (either p1 = contourf(xs, ys, gs_int(xs, ys)') or p1 = contourf(xs, ys, gs_int(xs, ys), transpose = true)