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

huangapple go评论84阅读模式
英文:

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 an equal aspect ratio of axes.

When I execute the following code:

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

I get:

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

And when I add this line to force the aspect ratio (as shown in the documentation):

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

I get the following error:

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

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

英文:

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

When I execute the following code:

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

I get:

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

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

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

I get the following error:

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

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

答案1

得分: 2

从你提供的文档中:

  1. axis = (autolimitaspect = 1, )

请注意数字 1 后面的逗号。这个逗号在创建命名元组时是必要的,当元组只有 一个单一元素 时,否则它等同于写成:

  1. axis = autolimitaspect = 1

这又等同于写成:

  1. autolimitaspect = 1
  2. axis = autolimitaspect

现在你可以看到为什么 autolimitaspect 突然变成了一个 Int

在创建单一元素元组时,尾随逗号也是必要的:

  1. julia> (3) |> typeof
  2. Int64
  3. julia> (3,) |> typeof
  4. Tuple{Int64}
英文:

From the documentation you linked to:

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

  1. axis = autolimitaspect = 1

which again is equivalent to writing

  1. autolimitaspect = 1
  2. axis = autolimitaspect

And now you see why autolimitaspect is suddenly an Int.

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

  1. julia> (3) |> typeof
  2. Int64
  3. julia> (3,) |> typeof
  4. Tuple{Int64}

huangapple
  • 本文由 发表于 2023年5月22日 17:54:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定