# CairoMakie cant plot with plot function and range problem

**URL:** https://discourse.julialang.org/t/cairomakie-cant-plot-with-plot-function-and-range-problem/99404
**Category:** New to Julia
**Tags:** question, package, plotting, makie, cairomakie
**Created:** [May 25, 2023, 8:03pm UTC](https://discourse.julialang.org/t/cairomakie-cant-plot-with-plot-function-and-range-problem/99404 "2023-05-25T20:03:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)
#### Post date: [May 25, 2023, 8:03pm UTC](https://discourse.julialang.org/t/cairomakie-cant-plot-with-plot-function-and-range-problem/99404/1 "2023-05-25T20:03:15Z")

</div>

I am trying to plot using below code and it is the same as tutorials. at first it couldnt recognize plot individually so i added CairoMakie at the function initials. but then it gives me the error of range couldnt be zero. can you help me?

```julia
using CairoMakie, GridapMakie
p0 = p_opt
vApp = vAppMax
pf_vec = pf_p0(p0; r, fem_params)
pfh = FEFunction(fem_params.Pf, pf_vec)
pth = (pf -> Threshold(pf; β, η)) ∘ pfh
A_mat = MatrixA(pth; fem_params)
b_vec = assemble_vector(0.0, fem_params.V0_Disp)
u_vec = A_mat \ b_vec
uh = FEFunction(fem_params.U_Disp, u_vec)

fig, ax, plt = CairoMakie.plot(fem_params.Ω, pth, colormap = :binary)
CairoMakie.Colorbar(fig[1,2], plt)
ax.aspect = AxisAspect(1)
ax.title = "Design Shape"
rplot = 110 # Region for plot
CairoMakie.limits!(ax, -rplot, rplot, (h1)/2-rplot, (h1)/2+rplot)
CairoMakie.save("shape.png", fig)

```

ArgumentError: range step cannot be zero

Stacktrace:  
[1] (::Colon)(start::Float32, step::Float32, stop::Float32)  
@ Base .\twiceprecision.jl:412  
[2] get\_minor\_tickvalues(i::IntervalsBetween, scale::Function, tickvalues::Vector{Float32}, vmin::Float32, vmax::Float32)  
@ Makie C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\lineaxis.jl:670  
[3] (::Makie.var"#1179#1192"{Attributes, Observable{Vector{Float32}}, Observable{Tuple{Float32, Float32}}})(tickvalues::Vector{Float32}, minorticks::IntervalsBetween)  
@ Makie C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\lineaxis.jl:394  
[4] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})  
@ Base .\essentials.jl:729  
[5] invokelatest(::Any, ::Any, ::Vararg{Any})  
@ Base .\essentials.jl:726  
[6] (::Observables.OnAny)(value::Any)  
@ Observables C:\Users\marya.julia\packages\Observables\PHGQ8\src\Observables.jl:415  
[7] #invokelatest#2  
@ .\essentials.jl:729 [inlined]  
[8] invokelatest  
@ .\essentials.jl:726 [inlined]  
[9] notify  
@ C:\Users\marya.julia\packages\Observables\PHGQ8\src\Observables.jl:169 [inlined]  
[10] setindex!(observable::Observable, val::Any)  
@ Observables C:\Users\marya.julia\packages\Observables\PHGQ8\src\Observables.jl:86  
[11] update\_tickpos\_string(closure\_args::Tuple{Observable{Vector{AbstractString}}, Observable{Vector{GeometryBasics.Point{2, Float32}}}, Observable{Vector{Float32}}, Observable{Tuple{Float32, Tuple{Float32, Float32}, Bool}}, Observable{Tuple{Float32, Float32}}}, tickvalues\_labels\_unfiltered::Tuple{Vector{Float64}, Vector{String}}, reversed::Bool, scale::typeof(identity))  
@ Makie C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\lineaxis.jl:191  
[12] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})  
@ Base .\essentials.jl:729  
[13] invokelatest(::Any, ::Any, ::Vararg{Any})  
@ Base .\essentials.jl:726  
[14] (::Observables.OnAny)(value::Any)  
@ Observables C:\Users\marya.julia\packages\Observables\PHGQ8\src\Observables.jl:415  
[15] #invokelatest#2  
@ .\essentials.jl:729 [inlined]  
[16] invokelatest  
@ .\essentials.jl:726 [inlined]  
[17] notify  
@ C:\Users\marya.julia\packages\Observables\PHGQ8\src\Observables.jl:169 [inlined]  
[18] setindex!(observable::Observable, val::Any)  
@ Observables C:\Users\marya.julia\packages\Observables\PHGQ8\src\Observables.jl:86  
[19] (::Observables.MapCallback)(value::Any)  
@ Observables C:\Users\marya.julia\packages\Observables\PHGQ8\src\Observables.jl:431  
[20] #invokelatest#2  
@ .\essentials.jl:729 [inlined]  
[21] invokelatest  
@ .\essentials.jl:726 [inlined]  
[22] notify(observable::Observables.AbstractObservable)  
@ Observables C:\Users\marya.julia\packages\Observables\PHGQ8\src\Observables.jl:169  
[23] Makie.LineAxis(parent::Scene, attrs::Attributes)  
@ Makie C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\lineaxis.jl:434  
[24] #LineAxis#1157  
@ C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\lineaxis.jl:3 [inlined]  
[25] initialize\_block!(cb::Colorbar)  
@ Makie C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\blocks\colorbar.jl:315  
[26] \_block(::Type{Colorbar}, ::Makie.Figure; bbox::Nothing, kwargs::Base.Pairs{Symbol, Observable{Any}, Tuple{Symbol, Symbol}, NamedTuple{(:colormap, :limits), Tuple{Observable{Any}, Observable{Any}}}})  
@ Makie C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\blocks.jl:403  
[27] _block(::Type{Colorbar}, ::GridPosition; kwargs::Base.Pairs{Symbol, Observable{Any}, Tuple{Symbol, Symbol}, NamedTuple{(:colormap, :limits), Tuple{Observable{Any}, Observable{Any}}}})  
@ Makie C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\blocks.jl:294  
[28] #_#1005  
@ C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\blocks.jl:279 [inlined]  
[29] Colorbar(fig\_or\_scene::GridPosition, plot::Combined{GridapMakie.meshfield, Tuple{BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, Oriented}, UnstructuredGrid{2, 2, Float64, Oriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, Gridap.CellData.OperationCellField{ReferenceDomain}}}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})  
@ Makie C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\blocks\colorbar.jl:29  
[30] Colorbar(fig\_or\_scene::GridPosition, plot::Combined{GridapMakie.meshfield, Tuple{BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, Oriented}, UnstructuredGrid{2, 2, Float64, Oriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, Gridap.CellData.OperationCellField{ReferenceDomain}}})  
@ Makie C:\Users\marya.julia\packages\Makie\Ppzqh\src\makielayout\blocks\colorbar.jl:21  
[31] top-level scope  
@ In[189]:13

i also did this for range but it gives me below error.

```julia
using CairoMakie, GridapMakie
p0 = p_opt
vApp = vAppMax
pf_vec = pf_p0(p0; r, fem_params)
pfh = FEFunction(fem_params.Pf, pf_vec)
pth = (pf -> Threshold(pf; β, η)) ∘ pfh
A_mat = MatrixA(pth; fem_params)
b_vec = assemble_vector(0.0, fem_params.V0_Disp)
u_vec = A_mat \ b_vec
uh = FEFunction(fem_params.U_Disp, u_vec)
cells = fem_params.Ω.cells

pth_array = [pth[cell] for cell in cells]
pth_min = minimum(pth_array)
pth_max = maximum(pth_array)

fig, ax, plt = CairoMakie.plot(fem_params.Ω, pth, colormap = :binary, range=(pth_min, pth_max))
CairoMakie.Colorbar(fig[1,2], plt)
ax.aspect = AxisAspect(1)
ax.title = "Design Shape"
rplot = 110 # Region for plot
CairoMakie.limits!(ax, -rplot, rplot, (h1)/2-rplot, (h1)/2+rplot)
CairoMakie.save("shape.png", fig)

```

type BodyFittedTriangulation has no field cells

Stacktrace:  
[1] getproperty(x::BodyFittedTriangulation{2, 2, UnstructuredDiscreteModel{2, 2, Float64, Oriented}, UnstructuredGrid{2, 2, Float64, Oriented, Nothing}, Gridap.Arrays.IdentityVector{Int64}}, f::Symbol)  
@ Base .\Base.jl:38  
[2] top-level scope  
@ In[187]:11

---

<div class="post-metadata">

### Author: ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)
#### Post date: [May 26, 2023, 9:32am UTC](https://discourse.julialang.org/t/cairomakie-cant-plot-with-plot-function-and-range-problem/99404/2 "2023-05-26T09:32:02Z")

</div>

This is probably a float precision error. You can try scaling your data up and then manually setting tick labels to avoid this.

> <https://github.com/MakieOrg/Makie.jl/issues/2298>
>
> Plotting small numbers can cause an error. I get this error fairly often. It com…plicates the construction of complex multi-axis plots when I have to deal with errors popping up in the middle so it would be nice if this could be avoided.
> 
> A reproducer was found \[on Discourse\](https://discourse.julialang.org/t/aog-range-step-cannot-be-zero/87624/12):
> 
> \`\`\`jl
> julia\> using GLMakie
> 
> julia\> b = 2.0^(-150)
> 7.006492321624085e-46
> 
> julia\> Float32(b) # notice that \`b\` is the smallest \`Float64\` such that \`Float32(b) != 0.0f0\`
> 1.0f-45
> 
> julia\> Float32(prevfloat(b)) # here you can see that efectively taking just the previous number bellow \`b\`, then \`Float32(b) == 0.0\`
> 0.0f0
> 
> julia\> lines(\[b, b\])
> Error showing value of type Makie.FigureAxisPlot:
> ERROR: ArgumentError: range step cannot be zero
> Stacktrace:
> \[1\] (::Colon)(start::Float32, step::Float32, stop::Float32)
> @ Base ./twiceprecision.jl:412
> \[2\] get\_minor\_tickvalues(i::IntervalsBetween, scale::Function, tickvalues::Vector{Float32}, vmin::Float32, vmax::Float32)
> @ Makie ~/.julia/packages/Makie/Ppzqh/src/makielayout/lineaxis.jl:670
> \[3\] (::Makie.var"#1179#1192"{Attributes, Observable{Vector{Float32}}, Observable{Tuple{Float32, Float32}}})(tickvalues::Vector{Float32}, minorticks::IntervalsBetween)
> @ Makie ~/.julia/packages/Makie/Ppzqh/src/makielayout/lineaxis.jl:394
> \[4\] (::Observables.var"#callback#13"{Makie.var"#1179#1192"{Attributes, Observable{Vector{Float32}}, Observable{Tuple{Float32, Float32}}}, Tuple{Observable{Vector{Float32}}, Observable{Any}}})(x::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:339
> \[5\] #invokelatest#2
> @ ./essentials.jl:729 \[inlined\]
> \[6\] invokelatest
> @ ./essentials.jl:726 \[inlined\]
> \[7\] notify
> @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:143 \[inlined\]
> \[8\] setindex!(observable::Observable, val::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:86
> \[9\] update\_tickpos\_string(closure\_args::Tuple{Observable{Vector{AbstractString}}, Observable{Vector{Point{2, Float32}}}, Observable{Vector{Float32}}, Observable{Tuple{Float32, Tuple{Float32, Float32}, Bool}}, Observable{Tuple{Float32, Float32}}}, tickvalues\_labels\_unfiltered::Tuple{Vector{Float64}, Vector{String}}, reversed::Bool, scale::typeof(identity))
> @ Makie ~/.julia/packages/Makie/Ppzqh/src/makielayout/lineaxis.jl:191
> \[10\] (::Observables.var"#callback#13"{typeof(Makie.update\_tickpos\_string), Tuple{Observable{Tuple{Observable{Vector{AbstractString}}, Observable{Vector{Point{2, Float32}}}, Observable{Vector{Float32}}, Observable{Tuple{Float32, Tuple{Float32, Float32}, Bool}}, Observable{Tuple{Float32, Float32}}}}, Observable{Tuple{Vector{Float64}, Vector{String}}}, Observable{Any}, Observable{Any}}})(x::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:339
> \[11\] #invokelatest#2
> @ ./essentials.jl:729 \[inlined\]
> \[12\] invokelatest
> @ ./essentials.jl:726 \[inlined\]
> \[13\] notify
> @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:143 \[inlined\]
> \[14\] setindex!(observable::Observable, val::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:86
> \[15\] #15
> @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:393 \[inlined\]
> \--- the last 6 lines are repeated 1 more time ---
> \[22\] (::Observables.var"#callback#13"{Observables.var"#15#16"{Makie.var"#1169#1182", Observable{Tuple{Float32, Float32}}}, Tuple{Observable{Any}}})(x::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:339
> \[23\] #invokelatest#2
> @ ./essentials.jl:729 \[inlined\]
> \[24\] invokelatest
> @ ./essentials.jl:726 \[inlined\]
> \[25\] notify
> @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:143 \[inlined\]
> \[26\] setindex!(observable::Observable, val::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:86
> \[27\] (::Observables.var"#6#7"{Observable{Any}})(x::Tuple{Float32, Float32})
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:114
> \[28\] #invokelatest#2
> @ ./essentials.jl:729 \[inlined\]
> \[29\] invokelatest
> @ ./essentials.jl:726 \[inlined\]
> \[30\] notify
> @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:143 \[inlined\]
> \[31\] setindex!(observable::Observable, val::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:86
> \[32\] #15
> @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:393 \[inlined\]
> \[33\] (::Observables.var"#callback#13"{Observables.var"#15#16"{typeof(Makie.ylimits), Observable{Tuple{Float32, Float32}}}, Tuple{Observable{GeometryBasics.HyperRectangle{2, Float32}}}})(x::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:339
> \[34\] #invokelatest#2
> @ ./essentials.jl:729 \[inlined\]
> \[35\] invokelatest
> @ ./essentials.jl:726 \[inlined\]
> \[36\] notify
> @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:143 \[inlined\]
> \[37\] setindex!(observable::Observable, val::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:86
> \[38\] adjustlimits!(la::Axis)
> @ Makie ~/.julia/packages/Makie/Ppzqh/src/makielayout/blocks/axis.jl:977
> \[39\] #1235
> @ ~/.julia/packages/Makie/Ppzqh/src/makielayout/blocks/axis.jl:498 \[inlined\]
> \[40\] (::Observables.var"#callback#13"{Makie.var"#1235#1265"{Axis}, Tuple{Observable{GeometryBasics.HyperRectangle{2, Int64}}, Observable{GeometryBasics.HyperRectangle{2, Float32}}}})(x::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:339
> \[41\] #invokelatest#2
> @ ./essentials.jl:729 \[inlined\]
> \[42\] invokelatest
> @ ./essentials.jl:726 \[inlined\]
> \[43\] notify
> @ ~/.julia/packages/Observables/ynr7h/src/Observables.jl:143 \[inlined\]
> \[44\] setindex!(observable::Observable, val::Any)
> @ Observables ~/.julia/packages/Observables/ynr7h/src/Observables.jl:86
> \[45\] reset\_limits!(ax::Axis; xauto::Bool, yauto::Bool, zauto::Bool)
> @ Makie ~/.julia/packages/Makie/Ppzqh/src/makielayout/blocks/axis.jl:623
> \[46\] reset\_limits!
> @ ~/.julia/packages/Makie/Ppzqh/src/makielayout/blocks/axis.jl:547 \[inlined\]
> \[47\] update\_state\_before\_display!(ax::Axis)
> @ Makie ~/.julia/packages/Makie/Ppzqh/src/makielayout/blocks/axis.jl:1351
> \[48\] update\_state\_before\_display!(f::Figure)
> @ Makie ~/.julia/packages/Makie/Ppzqh/src/figureplotting.jl:177
> \[49\] update\_state\_before\_display!
> @ ~/.julia/packages/Makie/Ppzqh/src/figureplotting.jl:173 \[inlined\]
> \[50\] backend\_display(s::Makie.FigureAxisPlot; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
> @ Makie ~/.julia/packages/Makie/Ppzqh/src/display.jl:50
> \[51\] backend\_display
> @ ~/.julia/packages/Makie/Ppzqh/src/display.jl:49 \[inlined\]
> \[52\] #display#932
> @ ~/.julia/packages/Makie/Ppzqh/src/display.jl:72 \[inlined\]
> \[53\] display(fig::Makie.FigureAxisPlot)
> @ Makie ~/.julia/packages/Makie/Ppzqh/src/display.jl:66
> \[54\] #invokelatest#2
> @ ./essentials.jl:729 \[inlined\]
> \[55\] invokelatest
> @ ./essentials.jl:726 \[inlined\]
> \[56\] print\_response(errio::IO, response::Any, show\_value::Bool, have\_color::Bool, specialdisplay::Union{Nothing, AbstractDisplay})
> @ REPL ~/.julia/juliaup/julia-1.8.1+0.x64/share/julia/stdlib/v1.8/REPL/src/REPL.jl:296
> \[57\] (::REPL.var"#45#46"{LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
> @ REPL ~/.julia/juliaup/julia-1.8.1+0.x64/share/julia/stdlib/v1.8/REPL/src/REPL.jl:278
> \[58\] with\_repl\_linfo(f::Any, repl::LineEditREPL)
> @ REPL ~/.julia/juliaup/julia-1.8.1+0.x64/share/julia/stdlib/v1.8/REPL/src/REPL.jl:521
> \[59\] print\_response(repl::AbstractREPL, response::Any, show\_value::Bool, have\_color::Bool)
> @ REPL ~/.julia/juliaup/julia-1.8.1+0.x64/share/julia/stdlib/v1.8/REPL/src/REPL.jl:276
> \[60\] (::REPL.var"#do\_respond#66"{Bool, Bool, REPL.var"#77#87"{LineEditREPL, REPL.REPLHistoryProvider}, LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
> @ REPL ~/.julia/juliaup/julia-1.8.1+0.x64/share/julia/stdlib/v1.8/REPL/src/REPL.jl:857
> \[61\] #invokelatest#2
> @ ./essentials.jl:729 \[inlined\]
> \[62\] invokelatest
> @ ./essentials.jl:726 \[inlined\]
> \[63\] run\_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
> @ REPL.LineEdit ~/.julia/juliaup/julia-1.8.1+0.x64/share/julia/stdlib/v1.8/REPL/src/LineEdit.jl:2510
> \[64\] run\_frontend(repl::LineEditREPL, backend::REPL.REPLBackendRef)
> @ REPL ~/.julia/juliaup/julia-1.8.1+0.x64/share/julia/stdlib/v1.8/REPL/src/REPL.jl:1248
> \`\`\`

---

<div class="post-metadata">

### Author: ![mary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mary/32/53139_2.png) [@mary](https://discourse.julialang.org/u/mary)
#### Post date: [May 29, 2023, 9:08pm UTC](https://discourse.julialang.org/t/cairomakie-cant-plot-with-plot-function-and-range-problem/99404/3 "2023-05-29T21:08:47Z")

</div>

thank you for your response. I have tried to understand and implement your response in my code but i couldn’t. can you explain more?
