# Contour plot on Structured grid using Meshes.jl

**URL:** https://discourse.julialang.org/t/contour-plot-on-structured-grid-using-meshes-jl/123854
**Category:** General Usage
**Tags:** glmakie, meshes, cairomakie
**Created:** [December 15, 2024, 6:48am UTC](https://discourse.julialang.org/t/contour-plot-on-structured-grid-using-meshes-jl/123854 "2024-12-15T06:48:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [December 15, 2024, 6:48am UTC](https://discourse.julialang.org/t/contour-plot-on-structured-grid-using-meshes-jl/123854/1 "2024-12-15T06:48:52Z")

</div>

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).

```julia
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](https://discourse.julialang.org/uploads/short-url/tbOZ4wV6jsvVWjRvhBGibzCLuZu.jl) (27.2 KB)  
[psi.jl](https://discourse.julialang.org/uploads/short-url/27ssQ2AKWVmD6svGyXUwULS6g9w.jl) (103.5 KB)  
[x.jl](https://discourse.julialang.org/uploads/short-url/jdmXMZCaxdfKpNDOYLq7a2gvBlU.jl) (98.8 KB)  
[z.jl](https://discourse.julialang.org/uploads/short-url/1b9IkQCrGebxu2S1zmL2KIzxhJi.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.

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [December 15, 2024, 9:33am UTC](https://discourse.julialang.org/t/contour-plot-on-structured-grid-using-meshes-jl/123854/2 "2024-12-15T09:33:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [December 15, 2024, 1:09pm UTC](https://discourse.julialang.org/t/contour-plot-on-structured-grid-using-meshes-jl/123854/3 "2024-12-15T13:09:07Z")

</div>

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

```julia
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)

```

---

<div class="post-metadata">

### Author: ![Herve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/herve/32/222014_2.png) [@Herve](https://discourse.julialang.org/u/Herve)
#### Post date: [December 19, 2024, 9:40pm UTC](https://discourse.julialang.org/t/contour-plot-on-structured-grid-using-meshes-jl/123854/4 "2024-12-19T21:40:36Z")

</div>

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)

```julia
#(...)

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

```

---

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [January 19, 2025, 6:18pm UTC](https://discourse.julialang.org/t/contour-plot-on-structured-grid-using-meshes-jl/123854/5 "2025-01-19T18:18:18Z")

</div>

Please write those code lines in one line simply as contour( x, z, z\_log, 10, …) .

```julia
for cl in Contour.levels(Contour.contours(x, z, z_log, 10))
	lvl = Contour.level(cl) # the z-value of this contour level
		for line in Contour.lines(cl)
			xs, ys = Contour.coordinates(line) # coordinates of this line segment
			lines!(ax, xs, ys, color=lvl, colorrange=ctrange, colormap=:jet)
			#text!(ax,position=Point2f0(mean(xs), mean(ys)), string(round(lvl, digits=2)), align = (:center, :center))
		end
end

```
