英文:
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:
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:
And when I add this line to force the aspect ratio (as shown in the documentation):
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?
英文:
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:
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 ?
答案1
得分: 2
从你提供的文档中:
axis = (autolimitaspect = 1, )
请注意数字 1
后面的逗号。这个逗号在创建命名元组时是必要的,当元组只有 一个单一元素 时,否则它等同于写成:
axis = autolimitaspect = 1
这又等同于写成:
autolimitaspect = 1
axis = autolimitaspect
现在你可以看到为什么 autolimitaspect
突然变成了一个 Int
。
在创建单一元素元组时,尾随逗号也是必要的:
julia> (3) |> typeof
Int64
julia> (3,) |> typeof
Tuple{Int64}
英文:
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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论