# Makie fails in my script but work in MWE

**URL:** <https://discourse.julialang.org/t/makie-fails-in-my-script-but-work-in-mwe/113594>\
**Category:** General Usage\
**Tags:** question, cairomakie\
**Created:** [April 28, 2024, 5:39pm UTC](https://discourse.julialang.org/t/makie-fails-in-my-script-but-work-in-mwe/113594 "2024-04-28T17:39:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [April 28, 2024, 5:39pm UTC](https://discourse.julialang.org/t/makie-fails-in-my-script-but-work-in-mwe/113594/1 "2024-04-28T17:39:37Z")

</div>

I have an MWE as follows:

```julia
using DataFrames
import CairoMakie as Mke

plotdf = DataFrame(
    nla=Int[1, 2, 3, 4, 5, 6],
    newLocalAuthority=String["Liverpool", "Knowsley", "St. Helens", "Sefton", "Wirral", "Halton"],
    rat=Float64[2.14229, 1.30896, 1.03733, 1.01917, 0.836269, 0.781173],
    col=Float64[0.330878, 0.116927, 0.0159174, 0.00824557, -0.0776542, -0.107253]
)

println(plotdf)

nticks=6
cr=extrema(plotdf.rat)
t_ft = "{:.1f}"
ax_lab = "Multiple of average funding\nper capita in England"

fig = Mke.Figure()
ax2 = Mke.Axis(fig[1, 1], yticks=(1:nticks, plotdf.newLocalAuthority), xtickformat=t_ft,
            limits=(cr, (0.5, nticks + 0.5)), xscale=log10, xticklabelsize=10.0f0, ygridvisible=false, xgridvisible=false,
            yticklabelsize=8.0f0, xlabel=ax_lab, xlabelsize=10.0f0,
            yticksvisible=false, yminorticks=0.5:1:nticks+0.5, yminorticksvisible=true, yminorticksize=5)
Mke.barplot!(ax2, plotdf.nla, plotdf.rat, direction=:x, color=plotdf.col)
Mke.display(fig)

```

which prints:

```julia
6×4 DataFrame
 Row │ nla newLocalAuthority rat col
     │ Int64 String Float64 Float64
─────┼─────────────────────────────────────────────────
   1 │ 1 Liverpool 2.14229 0.330878
   2 │ 2 Knowsley 1.30896 0.116927
   3 │ 3 St. Helens 1.03733 0.0159174
   4 │ 4 Sefton 1.01917 0.00824557
   5 │ 5 Wirral 0.836269 -0.0776542
   6 │ 6 Halton 0.781173 -0.107253

```

and produces this plot:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/1/917c91f5e952062ecfb5f4d3b758b220477a1960.png)

However, within my full script, this code:

```julia
function newplot(plotdf)
    println(plotdf)

    nticks = 6
    cr = extrema(plotdf.rat)
    t_ft = "{:.1f}"
    ax_lab = "Multiple of average funding\nper capita in England"

    fig = Mke.Figure()
    ax2 = Mke.Axis(fig[1, 1], yticks=(1:nticks, plotdf.newLocalAuthority), xtickformat=t_ft,
        limits=(cr, (0.5, nticks + 0.5)), xscale=log10, xticklabelsize=10.0f0, ygridvisible=false, xgridvisible=false,
        yticklabelsize=8.0f0, xlabel=ax_lab, xlabelsize=10.0f0,
        yticksvisible=false, yminorticks=0.5:1:nticks+0.5, yminorticksvisible=true, yminorticksize=5)
    Mke.barplot!(ax2, plotdf.nla, plotdf.rat, direction=:x, color=plotdf.col)
# Mke.barplot!(ax2, plotdf.nla, plotdf.rat, direction=:x)
    Mke.display(fig)

end

```

prints exactly the same data:

```julia
6×4 DataFrame
 Row │ nla newLocalAuthority rat col
     │ Int64 String? Float64? Float64?
─────┼─────────────────────────────────────────────────
   1 │ 6 Halton 0.781173 -0.107253
   2 │ 5 Wirral 0.836269 -0.0776542
   3 │ 4 Sefton 1.01917 0.00824557
   4 │ 3 St. Helens 1.03733 0.0159174
   5 │ 2 Knowsley 1.30896 0.116927
   6 │ 1 Liverpool 2.14229 0.330878

```

but errors thusly:

```julia
ERROR: LoadError: MethodError: no method matching set_source(::Cairo.CairoContext, ::Float32)

Closest candidates are:
  set_source(::Cairo.CairoContext, ::Cairo.CairoPattern)
   @ CairoMakie C:\Users\TGebbels\.julia\packages\CairoMakie\legqN\src\utils.jl:149
  set_source(::Cairo.CairoContext, ::Colorant)
   @ CairoMakie C:\Users\TGebbels\.julia\packages\CairoMakie\legqN\src\utils.jl:153

Stacktrace:
  [1] (::CairoMakie.var"#58#59"{CairoMakie.Screen{CairoMakie.IMAGE}})(shape::GeometryBasics.HyperRectangle{2, Float32}, c::Float32, sc::RGBA{Float32}, sw::Int64)
    @ CairoMakie C:\Users\TGebbels\.julia\packages\CairoMakie\legqN\src\overrides.jl:103
  [2] macro expansion
    @ C:\Users\TGebbels\.julia\packages\Makie\ND0gA\src\utilities\utilities.jl:213 [inlined]
  [3] broadcast_foreach(::CairoMakie.var"#58#59"{CairoMakie.Screen{CairoMakie.IMAGE}}, ::Vector{GeometryBasics.HyperRectangle{2, Float32}}, ::Vector{Float32}, ::RGBA{Float32}, ::Int64)
    @ Makie C:\Users\TGebbels\.julia\packages\Makie\ND0gA\src\utilities\utilities.jl:199
  [4] draw_poly(scene::Makie.Scene, screen::CairoMakie.Screen{CairoMakie.IMAGE}, poly::MakieCore.Poly{Tuple{Vector{GeometryBasics.HyperRectangle{2, Float32}}}}, shapes::Vector{GeometryBasics.HyperRectangle{2, Float32}})
    @ CairoMakie C:\Users\TGebbels\.julia\packages\CairoMakie\legqN\src\overrides.jl:101
  [5] draw_plot(scene::Makie.Scene, screen::CairoMakie.Screen{CairoMakie.IMAGE}, poly::MakieCore.Poly{Tuple{Vector{GeometryBasics.HyperRectangle{2, Float32}}}})
    @ CairoMakie C:\Users\TGebbels\.julia\packages\CairoMakie\legqN\src\overrides.jl:17
  [6] cairo_draw(screen::CairoMakie.Screen{CairoMakie.IMAGE}, scene::Makie.Scene)
    @ CairoMakie C:\Users\TGebbels\.julia\packages\CairoMakie\legqN\src\infrastructure.jl:51
  [7] display(screen::CairoMakie.Screen{CairoMakie.IMAGE}, scene::Makie.Scene; connect::Bool)
    @ CairoMakie C:\Users\TGebbels\.julia\packages\CairoMakie\legqN\src\display.jl:43
  [8] display(screen::CairoMakie.Screen{CairoMakie.IMAGE}, scene::Makie.Scene)
    @ CairoMakie C:\Users\TGebbels\.julia\packages\CairoMakie\legqN\src\display.jl:40
  [9] display(figlike::Makie.Figure; backend::Module, inline::MakieCore.Automatic, update::Bool, screen_config::@Kwargs{})
    @ Makie C:\Users\TGebbels\.julia\packages\Makie\ND0gA\src\display.jl:166
 [10] display
    @ C:\Users\TGebbels\.julia\packages\Makie\ND0gA\src\display.jl:130 [inlined]
 [11] newplot(plotdf::DataFrame)
    @ Main c:\Users\TGebbels\...\Documents\DCMS Database\CityMaps\GeoStat City Maps.jl:53
 [12] eachcitymap(fundergt::GeoTable{@NamedTuple{LADcode::SubArray{Union{Missing, String}, 1, Vector{Union{Missing, String}}, Tuple{Vector{Int64}}, false}, LAD23NM::SubArray{Union{Missing, String}, 1, Vector{Union{Missing, String}}, Tuple{Vector{Int64}}, false}, LAD23NMW::SubArray{Union{Missing, String}, 1, Vector{Union{Missing, String}}, Tuple{Vector{Int64}}, false}, BNG_E::SubArray{Union{Missing, Int64}, 1, Vector{Union{Missing, Int64}}, Tuple{Vector{Int64}}, false}, BNG_N::SubArray{Union{Missing, Int64}, 1, Vector{Union{Missing, Int64}}, Tuple{Vector{Int64}}, false}, LONG::SubArray{Union{Missing, Float64}, 1, Vector{Union{Missing, Float64}}, Tuple{Vector{Int64}}, false}, LAT::SubArray{Union{Missing, Float64}, 1, Vector{Union{Missing, Float64}}, Tuple{Vector{Int64}}, false}, GlobalID::SubArray{Union{Missing, String}, 1, Vector{Union{Missing, String}}, Tuple{Vector{Int64}}, false}, funder::SubArray{Union{Missing, String}, 1, Vector{Union{Missing, String}}, Tuple{Vector{Int64}}, false}, newLocalAuthority::SubArray{Union{Missing, String}, 1, Vector{Union{Missing, String}}, Tuple{Vector{Int64}}, false}, Total::SubArray{Union{Missing, Float64}, 1, Vector{Union{Missing, Float64}}, Tuple{Vector{Int64}}, false}, Pop::SubArray{Union{Missing, Int64}, 1, Vector{Union{Missing, Int64}}, Tuple{Vector{Int64}}, false}, rat::SubArray{Union{Missing, Float64}, 1, Vector{Union{Missing, Float64}}, Tuple{Vector{Int64}}, false}, col::SubArray{Union{Missing, Float64}, 1, Vector{Union{Missing, Float64}}, Tuple{Vector{Int64}}, false}}}, df::DataFrame, cr::Tuple{Float64, Float64})
    @ Main c:\Users\TGebbels\...\Documents\DCMS Database\CityMaps\GeoStat City Maps.jl:65
 [13] macro expansion
    @ .\timing.jl:279 [inlined]
 [14] main()
    @ Main c:\Users\TGebbels\...\Documents\DCMS Database\CityMaps\GeoStat City Maps.jl:198
 [15] top-level scope
    @ c:\Users\TGebbels\...\Documents\DCMS Database\CityMaps\GeoStat City Maps.jl:205
in expression starting at c:\Users\TGebbels\...\Documents\DCMS Database\CityMaps\GeoStat City Maps.jl:205

```

If I remove the `color=plotdf.col` option from the `barplot!` command, then it works fine, just without the colors.

Nothing else related to Makie runs in my script before this invocation.  
Can anyone suggest what might be causing this?

---

<div class="post-metadata">

**Author:** ![aryavorskiy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aryavorskiy/32/43466_2.png) [@aryavorskiy](https://discourse.julialang.org/u/aryavorskiy)\
**Post date:** [April 28, 2024, 7:35pm UTC](https://discourse.julialang.org/t/makie-fails-in-my-script-but-work-in-mwe/113594/2 "2024-04-28T19:35:41Z")

</div>

> [@TimG](#):
>
> ```julia
> 6×4 DataFrame
> Row │ nla newLocalAuthority rat col
> │ Int64 String? Float64? Float64?
> ─────┼─────────────────────────────────────────────────
> 1 │ 6 Halton 0.781173 -0.107253
> 2 │ 5 Wirral 0.836269 -0.0776542
> 3 │ 4 Sefton 1.01917 0.00824557
> 4 │ 3 St. Helens 1.03733 0.0159174
> 5 │ 2 Knowsley 1.30896 0.116927
> 6 │ 1 Liverpool 2.14229 0.330878
> 
> ```

Hi!

Note that the table printed in the second case is not _exactly_ the same as in your MWE: some columns have question marks after their types. Probably you worked with `missing` values before.

This might cause dispatch issues on Makie side. Try running `disallowmissing!(plotdf)` in the beginning of your function. Does it fix your problem?

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [April 28, 2024, 9:42pm UTC](https://discourse.julialang.org/t/makie-fails-in-my-script-but-work-in-mwe/113594/3 "2024-04-28T21:42:36Z")

</div>

Well, yes, that worked! Thank you!

What an amazing place this is that I can get speedy answers even on a Sunday evening!

I took precautions to remove actual missing values from my data, and this was enough for `viz!` and `Colorbar!`, which I’m also using in the same `fig`, but not for `barplot!` it seems. I wonder why `barplot!` is more fussy.

Also, unpicking this stack dump to understand the actual cause was somewhat challenging for me, I must say.

Thanks again.
