Makie fails in my script but work in MWE

I have an MWE as follows:

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:

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:

However, within my full script, this code:

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:

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:

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?

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?

2 Likes

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.

1 Like