Search code examples
juliamakie.jl

Cannot `convert` an object of type Int64 to an object of type Symbol with CairoMakie and Julia


I want to create pie charts with julia and cairomakie with a equal aspect ratio of axis.

When I execute the following code:

using CairoMakie
fig, ax, plt =Makie.pie([5,7,1,4],
                 color = [:yellow, :orange, :red, :blue],
                 radius = 4,
                 inner_radius = 2,
                 strokecolor = :black,
                 strokewidth = 2,
                )
save("myfigure.png", fig)

I get:

enter image description here

and when I add this line to force the aspect ratio (as show in https://docs.makie.org/stable/examples/plotting_functions/pie/):

fig, ax, plt =Makie.pie([5,7,1,4],
                 color = [:yellow, :orange, :red, :blue],
                 radius = 4,
                 inner_radius = 2,
                 strokecolor = :black,
                 strokewidth = 2,
                 axis = (autolimitaspect = 1) # line added
                )

I get the following error:

ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type Symbol
Closest candidates are:
  convert(::Type{T}, ::T) where T at essentials.jl:171
  Symbol(::Any...) at strings/basic.jl:227

Do you have an idea of what's going wrong with my command ?


Solution

  • From the documentation you linked to:

    axis = (autolimitaspect = 1, )
    

    Note the trailing comma after the number 1. This comma is necessary to create a named tuple, when the tuple has only a single element, otherwise, it is just equal to writing

    axis = autolimitaspect = 1
    

    which again is equivalent to writing

    autolimitaspect = 1
    axis = autolimitaspect
    

    And now you see why autolimitaspect is suddenly an Int.

    The trailing comma is also necessary when creating single-element tuples:

    julia> (3) |> typeof
    Int64
    
    julia> (3,) |> typeof
    Tuple{Int64}