Contour plot on Structured grid using Meshes.jl

In Julia i am using Meshes.jl and GLMakie.jl .
I am using x , z, alfsurf and psi variables of size (109,50) each.
I input them to StructuredGrid() function which creates a structured grid of size (108,49).

using Meshes, GLMakie

x = include("x.jl")
z = include("z.jl")
psi = include("psi.jl")
alfsurf = include("alfsurf.jl")

grd = StructuredGrid(x, z)

function safe_log(x)
	x == 0 ? 1 : log(x)
end
z_log = safe_log.(vec(alfsurf))
fig, ax, plt = Meshes.viz(grd, color=z_log, colorscheme=:jet)
contour!(plt,grd, psi, levels =15,color=:red)

How can i plot contours of psi although my code plots heatmap of alfsurf correctly ? Data of variables x, z, psi and alfsurf is in attached files.
alfsurf.jl (27.2 KB)
psi.jl (103.5 KB)
x.jl (98.8 KB)
z.jl (97.5 KB)

I got error message shown below:

ERROR: BoundsError: attempt to access 2-element Vector{Observable} at index [1:3]
Stacktrace:
[1] throw_boundserror(A::Vector{Observable}, I::Tuple{UnitRange{Int64}})
@ Base ./essentials.jl:14
[2] checkbounds
@ ./abstractarray.jl:699 [inlined]
[3] getindex
@ ./array.jl:936 [inlined]
[4] getindex
@ ~/.julia/packages/MakieCore/05FeT/src/attributes.jl:184 [inlined]
[5] plot!(plot::Plot{contour, Tuple{StructuredGrid{𝔼{…}, CoordRefSystems.Cartesian2D{…}, 2, Tuple{…}}, Matrix{Float64}}})
@ Makie ~/.julia/packages/Makie/tjqse/src/basic_recipes/contours.jl:212
[6] connect_plot!(parent::Plot{viz, Tuple{StructuredGrid{…}}}, plot::Plot{contour, Tuple{StructuredGrid{…}, Matrix{…}}})
@ Makie ~/.julia/packages/Makie/tjqse/src/interfaces.jl:395
[7] plot!(scene::Plot{viz, Tuple{StructuredGrid{…}}}, plot::Plot{contour, Tuple{StructuredGrid{…}, Matrix{…}}})
@ Makie ~/.julia/packages/Makie/tjqse/src/interfaces.jl:412
[8] _create_plot!(::Function, ::Dict{…}, ::Plot{…}, ::StructuredGrid{…}, ::Vararg{…})
@ Makie ~/.julia/packages/Makie/tjqse/src/figureplotting.jl:391
[9] contour!(::Plot{viz, Tuple{StructuredGrid{…}}}, ::Vararg{Any}; kw::@Kwargs{levels::Int64, color::Symbol})
@ Makie ~/.julia/packages/MakieCore/05FeT/src/recipes.jl:505
[10] top-level scope
@ REPL[22]:1
Some type information was truncated. Use show(err) to see complete types.

Hi @raman_kumar,

The visualization recipes in Meshes.jl handle the color-per-element and color-per-vertex cases. The contour plot is a plot that performs additional operations with the input mesh. Namely, it computes the contour geometries.

You could try to compute the contours explicitly using Contours.jl or use the builtin Makie.jl functions to plot contours.

@juliohm Please can you suggest some example or resources for creating contours using GLMakie.jl ? I have to plot the data as a heatmap and then contour above it. I used CairoMakie but it is too slow. See this CairoMakie.jl code below:

using CairoMakie

x = vec(include("x.jl"))
z = vec(include("z.jl"))
alfsurf = include("alfsurf.jl")

function safe_log(x)
	x == 0 ? 1 : log(x)
end
z_log = safe_log.(vec(alfsurf))
fig = Figure(size = (800, 600))
ax = Axis(fig[1, 1],
	title = "Density and Contour Plot",
	xlabel = L"x_{R}",
	ylabel = L"z_{R}",
	aspect = DataAspect())

# Plot the data as a heatmap and then contour
heatmap!(ax, x, z, z_log, colormap = :jet)
xlims!(ax, minimum(x), maximum(x))
ylims!(ax, minimum(z), maximum(z))
save("Demo.svg", fig)

If you use Contour.jl, things go down pretty smoothly (straight from the Contour.jl documentation) (the “import Contour as CT” avoids conflicts with other definitions of contour in Makie or other graphics packages)

#(...)

import Contour as CT

#(...)

psirange = (minimum(safe_log.(psi)), maximum(safe_log.(psi)))
for cl in CT.levels(CT.contours(x, z, safe_log.(psi), 10))
    lvl = CT.level(cl) # the z-value of this contour level
	@info "Level: $lvl"
    for line in CT.lines(cl)
        xs, ys = CT.coordinates(line) # coordinates of this line segment
        lines!(ax, xs, ys, color=lvl, colorrange=psirange, colormap=:jet) # pseudo-code; use whatever plotting package you prefer
    end
end
fig
1 Like