Engee 文档

传说

该页面正在翻译中。

从元素创建图例

您可以通过传递图例条目向量和标签向量以及可选标题作为第三个参数来创建基本图例。

图例条目向量中的元素可以是plot对象,也可以是Lineelement、MarkerElement和PolyElement等Legendelement。 或者它们可以是将作为一个分层在一起的此类对象的向量。

图例元素属性

标准绘图对象如 散点,散点线条 有预定义的转换到 标记,标记s和 行距;行距将相关情节属性复制到图例元素的s。 如果属性具有类似矢量的值,则会回退到图例的标量默认值。 默认情况下,图例默认值本身是从主主题继承的。 例如, 扩音器,扩音器 传说又回到了 贴片宽度 的主旋律。 在下面的示例中,您可以看到 sca2 复制 :rect 标记但不是矢量值颜色。 </无翻译>

using CairoMakie

f = Figure()

Axis(f[1, 1])

xs = 0:0.5:10
ys = sin.(xs)
lin = lines!(xs, ys, color = :blue)
sca = scatter!(xs, ys, color = :red)
sca2 = scatter!(xs, ys .+ 0.5, color = 1:length(xs), marker = :rect)

Legend(f[1, 2],
    [lin, sca, [lin, sca], sca2],
    ["a line", "some dots", "both together", "rect markers"])

f
1f7747e

从轴创建图例

您也可以通过传递一个轴对象来创建一个图例,如 轴心,轴心, LScene,LScene场景. 所有有 标签 属性集将按照它们在轴中出现的顺序放入图例中,并且您可以选择将标题作为第三个参数传递。 </无翻译>

using CairoMakie

f = Figure()

ax = f[1, 1] = Axis(f)

lines!(0..15, sin, label = "sin", color = :blue)
lines!(0..15, cos, label = "cos", color = :red)
lines!(0..15, x -> -cos(x), label = "-cos", color = :green)

f[1, 2] = Legend(f, ax, "Trig Functions", framevisible = false)

f
2f8ee39

与关键字 合并;合并独特的 您可以控制如何处理具有相同标签的绘图对象。 如果 合并;合并真的,所有具有相同标签的绘图对象将被分层到一个图例条目中。 如果 独特的真的,具有相同情节类型和标签的所有情节对象将减少到一次出现。 </无翻译>

using CairoMakie

f = Figure()

traces = cumsum(randn(10, 5), dims = 1)

for (i, (merge, unique)) in enumerate(
        Iterators.product([false, true], [false true]))

    axis = Axis(f[fldmod1(i, 2)...],
        title = "merge = $merge, unique = $unique")

    for trace in eachcol(traces)
        lines!(trace, label = "single", color = (:black, 0.2))
    end

    mu = vec(sum(traces, dims = 2) ./ 5)
    lines!(mu, label = "mean")
    scatter!(mu, label = "mean")

    axislegend(axis, merge = merge, unique = unique)

结束

f
674be45

轴内的图例

n.轴,轴 函数是向轴添加图例的快速方法。 您可以传递一个选定的轴加上参数,这些参数被转发到 传说 构造函数,或者默认使用当前轴。 如果您只传递一个字符串,它将用作当前轴的标题。

位置可以通过一个快捷符号来设置,首先是halign(l,r,c),然后是valign(b,t,c),例如:lt表示左边,顶部和:cb表示中间底部。 或者您可以使用元素从0到1的元组(halign,valign)设置位置: 位置=(0.1,0.9) </无翻译>

using CairoMakie

f = Figure()

ax = Axis(f[1, 1])

sc1 = scatter!(randn(10, 2), color = :red, label = "Red Dots")
sc2 = scatter!(randn(10, 2), color = :blue, label = "Blue Dots")
scatter!(randn(10, 2), color = :orange, label = "Orange Dots")
scatter!(randn(10, 2), color = :cyan, label = "Cyan Dots")

axislegend()

axislegend("Titled Legend", position = :lb)

axislegend(ax, [sc1, sc2], ["One", "Two"], "Selected Dots", position = :rb,
    orientation = :horizontal)

f
3632d22

或者,您可以简单地将图例添加到轴所在的同一布局槽中。 只要轴大于图例,您就可以设置图例的 告诉我们告诉宽度错误 并使用对齐变量定位它。 您可以使用margin关键字来防止图例与轴棘接触。 </无翻译>

using CairoMakie

haligns = [:left, :right, :center]
valigns = [:top, :bottom, :center]

f = Figure()

Axis(f[1, 1])

xs = 0:0.1:10
lins = [lines!(xs, sin.(xs .* i), color = color)
    for (i, color) in zip(1:3, [:red, :blue, :green])]

for (j, ha, va) in zip(1:3, haligns, valigns)
    Legend(
        f[1, 1], lins, ["Line $i" for i in 1:3],
        "$ha & $va",
        tellheight = false,
        tellwidth = false,
        margin = (10, 10, 10, 10),
        halign = ha, valign = va, orientation = :horizontal
    )
end

f
be2d297

手动创建图例条目

有时您可能希望从头开始构建图例条目以获得最大程度的控制。 到目前为止,您可以使用 线程;线程s, 标记,标记s或 多元元素这些元素的属性如下( [] 直接构造这些元素时可以省略部分,但必须为图例所包含的属性完全写出):

# LineElement
[line]points, [line]color, linestyle, linewidth

# MarkerElement
[marker]points, marker, markersize, [marker]color,
[marker]strokewidth, [marker]strokecolor

# PolyElement
[poly]points, [poly]color, [poly]strokewidth, [poly]strokecolor

属性 线点,线点, 标记点多点剂,多点剂 确定图例条目patch矩形中绘图对象的位置。 这些值应归一化为1乘1矩形,最终形状取决于 补丁大小 的传说。 例如,如果你想要更宽的线和多标记,你可以设置 补丁大小 传说中的 (50, 30). </无翻译>

using CairoMakie

f = Figure()

轴(f[1,1])

elem_1=[LineElement(color=:red,linestyle=nothing),
          MarkerElement(color=:blue,marker='x',markersize=15,
          strokecolor=:黑色)]

elem_2=[多元元素(颜色=:红色,strokecolor=:蓝色,strokewidth=1),
          LineElement(color=:black,linestyle=:dash)]

elem_3=LineElement(color=:green,linestyle=nothing,
        积分=Point2f[(0, 0), (0, 1), (1, 0), (1, 1)])

elem_4=MarkerElement(颜色=:蓝色,标记='π',markersize=15,
        积分=Point2f[(0.2, 0.2), (0.5, 0.8), (0.8, 0.2)])

elem_5=多元元素(颜色=:绿色,strokecolor=:黑色,strokewidth=2,
        积分=Point2f[(0, 0), (1, 0), (0, 1)])

图例(f[1,2],
    [elem_1,elem_2,elem_3,elem_4,elem_5],
    ["线和标记","Poly&Line","线","标记","Poly"],
    patchsize=(35,35),rowgap=10)

f
728f62a

复盖图例条目属性

默认情况下,图例继承其所属图的视觉属性。 有时,有必要复盖其中一些属性以使图例更清晰。 您可以将键值对象配对,如 命名的,命名的 或一个 Dict{Symbol} 到一个情节的 标签 要复盖其自动图例条目,例如增加a的标记大小 散点,散点: </无翻译>

using CairoMakie
f, ax, sc = scatter(
    cos.(range(0, 7pi, 100)),
    color = :black,
    markersize = 8,
    label = "cos" => (; markersize = 15)
)
scatter!(
    sin.(range(0, 7pi, 100)),
    color = :black,
    marker = :utriangle,
    markersize = 8,
    label = "sin" => (; markersize = 15)
)
Legend(f[1, 2], ax)
f
ab60c21

这些是您可以复盖的属性(请注意,其中一些具有方便别名,如 颜色 它适用于所有元素,而 多色,多色 只适用于 多元元素s):

  • 标记,标记

    • [标记]点, 标记大小, [标记]strokewidth, [标记]颜色, [标记]strokecolor, [标记]颜色范围, [标记]颜色表

  • 线程;线程

    • [线]点, 线宽, [线]颜色, 林泳,林泳, [线]颜色范围, [线]颜色表

  • 多元元素

    • [聚]点, [聚]strokewidth, [聚]颜色, [聚]strokecolor, [多]颜色变化, [聚]颜色表

另一种常见情况是,当您要为具有分类颜色图的图创建图例时。 通过传递与复盖配对的标签向量,您可以创建具有正确颜色的多个条目: </无翻译>

using CairoMakie
f, ax, bp = barplot(
    1:5,
    [1, 3, 2, 5, 4],
    color = 1:5,
    colorrange = (1, 5),
    colormap = :Set1_5,
    label = [label => (; color = i)
        for (i, label) in enumerate(["red", "blue", "green", "purple", "orange"])]
)
Legend(f[1, 2], ax)
f
2bcd8a9

您也可以复盖 传说 构造函数本身,在这种情况下,您将复盖与要复盖其图例条目的图配对: </无翻译>

using CairoMakie
f = Figure()
ax = Axis(f[1, 1])
li = lines!(ax, 1:5, linestyle = :dot)
sc = scatter!(ax, 1:5, markersize = 10)
Legend(
    f[1, 2],
    [
        sc => (; markersize = 20),
        li => (; linewidth = 3),
        [li, sc] => (; color = :red),
        [li => (; linewidth = 3), sc => (; markersize = 20)],
    ],
    ["Scatter", "Line", "Both", "Both 2"],
    patchsize = (40, 20),
)
f
9c9bc43

多组传奇

有时一个图例由多个组组成,例如在一个图中,标记大小和颜色都是不同的,这些属性需要单独可视化,但仍然在一个图例中一起显示。 每个组的内容以元素数组和标签数组的形式给出,每个元素都在一个集体数组中。 您可以使用 [医]定位 属性,要么 :左:顶部. </无翻译>

using CairoMakie

f = Figure()

markersizes = [5, 10, 15, 20]
colors = [:red, :green, :blue, :orange]

group_size = [MarkerElement(marker = :circle, color = :black,
    strokecolor = :transparent,
    markersize = ms) for ms in markersizes]

group_color = [PolyElement(color = color, strokecolor = :transparent)
    for color in colors]

legends = [Legend(f,
    [group_size, group_color],
    [string.(markersizes), string.(colors)],
    ["Size", "Color"], tellheight = true) for _ in 1:4]

f[1, 1:2] = legends[1:2]
f[2, :] = legends[3]
f[3, :] = legends[4]

for l in legends[3:4]
    l.orientation = :horizontal
    l.tellheight = true
    l.tellwidth = false
end

legends[2].titleposition = :left
legends[4].titleposition = :left

传说[1]。nbanks=2
传说[4]。nbanks=2

Label(f[1,1,Left()],"titleposition=:top\norientation=:vertical\nnbanks=2",font=:italic,padding= (0, 10, 0, 0))
Label(f[1,2,Right()],"titleposition=:left\norientation=:vertical\nnbanks=1",font=:italic,padding= (10, 0, 0, 0))
Label(f[2,1:2,Top()],"titleposition=:top,orientation=:horizontal\nnbanks=1",font=:italic)
Label(f[3,1:2,Top()],"titleposition=:left,orientation=:horizontal\nnbanks=2",font=:italic)

f
487f416

属性

对齐模式

默认值为 里面()

图例在其父GridLayout中的对齐模式。

阿尔法

默认值为 1

图例元素的默认alpha

背景颜色

默认值为 :白色

图例的背景颜色。

[医]背景色

默认值为 什么都没有

图例的背景颜色。 弃用-使用 背景颜色 相反。

科尔加普

默认值为 16

一个图例条目的标签和下一个图例条目的补丁之间的间隙。 </无翻译>

using CairoMakie
fig = Figure()
ax = Axis(fig[1, 1])
lines!(ax, 1:10, linestyle = :dash, label = "Line")
poly!(ax, [(5, 0), (10, 0), (7.5, 5)], label = "Poly")
scatter!(ax, 4:13, label = "Scatter")
Legend(fig[1, 2], ax, "Default", nbanks = 2)
Legend(fig[1, 3], ax, "colgap = 40", nbanks = 2, colgap = 40)
fig
ec51dd9

框架颜色

默认值为 :黑色

图例边框的颜色。

框架可分

默认值为 真的

控制图例边框是否可见。

框架宽度

默认值为 1.0

图例边框的线宽。

网格标记

默认值为 :中心

条目组在其父GridLayout中的水平对齐方式。

网格值

默认值为 :中心

条目组在其父GridLayout中的垂直对齐方式。

团体/团体

默认值为 16

每组和下一组之间的间隙。 </无翻译>

using CairoMakie
fig = Figure()
ax = Axis(fig[1, 1])
lin = lines!(ax, 1:10, linestyle = :dash)
pol = poly!(ax, [(5, 0), (10, 0), (7.5, 5)])
sca = scatter!(ax, 4:13)
Legend(fig[1, 2],
    [[lin], [pol], [sca]],
    [["Line"], ["Poly"], ["Scatter"]],
    ["Default", "Group 2", "Group 3"];

)
Legend(fig[1, 3],
    [[lin], [pol], [sca]],
    [["Line"], ["Poly"], ["Scatter"]],
    ["groupgap = 30", "Group 2", "Group 3"];
    groupgap = 30,
)
fig
c8ccb89

哈林

默认值为 :中心

图例在其建议的边界框中的水平对齐。

[医]热变化

默认值为 自动的

HeatmapElements的默认colorrange。 如果没有设置,这将从heatmapvalues派生。

[医]热

默认值为 (0 .. 1, 0 .. 1)

在相对于每个标签补丁的标准化坐标中,用于热图元素的默认(x,y)限制(或向量)。

热值;热值

默认值为 [0 0.3; 0.6 1]

用于HeatmapElements的默认值。

身高

默认值为 汽车()

图例的高度设置。

影像或影像

默认值为 自动的

ImageElements的默认colorrange。 如果没有设置,这将从imagevalues派生。

意象,意象

默认值为 (0 .. 1, 0 .. 1)

用于相对于每个标签贴片的标准化坐标中的ImageElements的默认(x,y)限制。

意象,意象

默认值为 [0 0.3; 0.6 1]

用于ImageElements的默认值(或颜色)。

标签

默认值为 "未定义"

默认条目标签。

[医]标签色

默认值为 @继承:textcolor:黑色

条目标签的颜色。

拉贝尔丰特

默认值为 :普通

条目标签的字体系列。

[医]标签

默认值为 :左

条目标签的水平对齐。

标签强化

默认值为 自动的

标签文本的对齐方式。 默认值为 自动的,这将设置理由labelhalign。

标签尺寸

默认值为 @继承:字体大小16.0f0

条目标签的字体大小。

标签价值

默认值为 :中心

条目标签的垂直对齐。

线色,线色

默认值为 主题(场景,:linecolor)

用于LineElements的默认线条颜色

[医]线针

默认值为 主题(场景,:colormap)

LineElements的默认颜色表

[医]线色

默认值为 自动的

LineElements的默认colorrange

线点,线点

默认值为 [Point2f(0,0.5),Point2f(1,0.5)]

相对于每个标签贴片的标准化坐标中用于LineElements的默认点。

林泳,林泳

默认值为 :固体

用于LineElements的默认线条样式

线宽

默认值为 主题(场景,:线宽)

用于LineElements的默认线宽。

保证金

默认值为 (0.0f0,0.0f0,0.0f0,0.0f0)

图例与其建议的boundingbox之间的额外空间。

标记物

默认值为 主题(场景,:标记)

MarkerElements的默认标记

[医]标记色

默认值为 主题(场景,:markercolor)

MarkerElements的默认标记颜色

markercolormap,markercolormap

默认值为 主题(场景,:colormap)

MarkerElements的默认标记颜色表

markercolorrange,markercolorrange

默认值为 自动的

MarkerElements的默认标记colorrange

标记点

默认值为 [点2f(0.5,0.5)]

相对于每个标签贴片的标准化坐标中用于MarkerElements的默认标记点。

标记大小

默认值为 主题(场景,:markersize)

用于MarkerElements的默认标记大小。

n.标记色,标记色

默认值为 主题(场景,:markerstrokecolor)

用于MarkerElements的默认标记笔划颜色。

马克斯特罗克宽

默认值为 主题(场景,:markerstrokewidth)

用于MarkerElements的默认标记笔划宽度。

网格;网格

默认值为 Rect3f(Point3f(-0.7),Vec3f(1.4))

用于网格元素的默认网格。 对于3D元素,摄像机位于(1,1,1)处,朝向(0,0,0),z向上。

[医]网色

默认值为 (wong_colors())[1]

用于网格元素的默认网格颜色。

[医]网格色

默认值为 主题(场景,:colormap)

网格元素的默认颜色表

网格颜色范围

默认值为 自动的

网格元素的默认颜色范围。

[医]网状色

默认值为 主题(场景,:markercolor)

MeshScatterElements的默认标记颜色

[医]网格色

默认值为 主题(场景,:colormap)

MeshScatterElements的默认标记颜色表

[医]网状颜色

默认值为 自动的

MeshScatterElements的默认标记colorrange

[医]网状捕手

默认值为 球体(Point3f(0),1.0f0)

MeshScatterElements的默认标记

网格点,网格点

默认值为 [第3f点(0)]

用于MeshScatterElements的默认标记点。 对于3D元素,摄像机位于(1,1,1)处,朝向(0,0,0),z向上。

[医]网状扩张

默认值为 四元数(0,0,0,1)

用于MeshScatterElements的默认标记旋转。

网格大小

默认值为 0.8

用于MeshScatterElements的默认标记大小。

nbanks

默认值为 1

图例条目分组的银行数。 如果图例是垂直方向的,则列,否则行。 </无翻译>

using CairoMakie
fig = Figure()
ax = Axis(fig[1, 1])
lines!(ax, 1:10, linestyle = :dash, label = "Line")
poly!(ax, [(5, 0), (10, 0), (7.5, 5)], label = "Poly")
scatter!(ax, 4:13, label = "Scatter")
grid = GridLayout(fig[1, 2], tellheight = false)
Legend(grid[1, 1], ax, "nbanks = 1", nbanks = 1, tellheight = true)
Legend(grid[1, 2], ax, "nbanks = 2", nbanks = 2, tellheight = true)
Legend(grid[2, :], ax, "nbanks = 3", nbanks = 3, tellheight = true)
fig
f3088ee

</无翻译>

using CairoMakie
fig = Figure()
ax = Axis(fig[1, 1])
lines!(ax, 1:10, linestyle = :dash, label = "Line")
poly!(ax, [(5, 0), (10, 0), (7.5, 5)], label = "Poly")
scatter!(ax, 4:13, label = "Scatter")
grid = GridLayout(fig[2, 1], tellwidth = false)
Legend(grid[1, 1], ax, "nbanks = 1", nbanks = 1,
    orientation = :horizontal, tellwidth = true)
Legend(grid[2, 1], ax, "nbanks = 2", nbanks = 2,
    orientation = :horizontal, tellwidth = true)
Legend(grid[:, 2], ax, "nbanks = 3", nbanks = 3,
    orientation = :horizontal, tellwidth = true)
fig
190c9de

方向感

默认值为 :垂直

图例的方向(:水平或:垂直)。 </无翻译>

using CairoMakie
fig = Figure()
ax = Axis(fig[1, 1])
lines!(ax, 1:10, linestyle = :dash, label = "Line")
poly!(ax, [(5, 0), (10, 0), (7.5, 5)], label = "Poly")
scatter!(ax, 4:13, label = "Scatter")
Legend(fig[2, 1], ax, "orientation
= :horizontal", orientation = :horizontal)
Legend(fig[1, 2], ax, "orientation
= :vertical", orientation = :vertical)
fig
fddfe34

填充物

默认值为 缧6.0f0缧6.0f0缧6.0f0缧6.0f0缧6.0f0缧6.0f0缧6.0f0缧6.0f0缧

图例内容与边框之间的额外空间。

贴片色,贴片色

默认值为 :透明

包含图例标记的色块的颜色。

[医]贴片,贴片

默认值为 5

补丁和每个图例条目的标签之间的间隙。 </无翻译>

using CairoMakie
fig = Figure()
ax = Axis(fig[1, 1])
lines!(ax, 1:10, linestyle = :dash, label = "Line")
poly!(ax, [(5, 0), (10, 0), (7.5, 5)], label = "Poly")
scatter!(ax, 4:13, label = "Scatter")
Legend(fig[1, 2], ax, "Default")
Legend(fig[1, 3], ax, "patchlabelgap
= 20", patchlabelgap = 20)
fig
f0be106

补丁大小

默认值为 (20.0f0,20.0f0)

包含图例标记的矩形的大小。 如果线条图案在默认大小下不能清晰可见,它可以帮助增加宽度。 </无翻译>

using CairoMakie
fig = Figure()
ax = Axis(fig[1, 1])
lines!(ax, 1:10, linestyle = :dash, label = "Line")
poly!(ax, [(5, 0), (10, 0), (7.5, 5)], label = "Poly")
scatter!(ax, 4:13, label = "Scatter")
Legend(fig[1, 2], ax, "Default")
Legend(fig[1, 3], ax, "(40, 20)", patchsize = (40, 20))
fig
fb371f6

[医]贴片色

默认值为 :透明

包含图例标记的色块边框的颜色。

贴片宽度

默认值为 1.0

包含图例标记的贴片边框的线宽。

多色,多色

默认值为 主题(场景,:patchcolor)

用于聚元素的默认聚色。

polycolormap,polycolormap

默认值为 主题(场景,:colormap)

多元元素的默认颜色表

polycolorrange,polycolorrange

默认值为 自动的

多元元素的默认颜色范围

多点剂,多点剂

默认值为 [Point2f(0,0),Point2f(1,0),Point2f(1,1),Point2f(0,1)]

在相对于每个标签贴片的归一化坐标中用于多元素的默认多点。

[医]聚酯纤维

默认值为 主题(场景,:patchstrokecolor)

用于多元素的默认多笔画颜色。

扩音器,扩音器

默认值为 主题(场景,:patchstrokewidth)

用于聚元素的默认聚笔画宽度。

rowgap,rowgap

默认值为 3

入行之间的间隙。 </无翻译>

using CairoMakie
fig = Figure()
ax = Axis(fig[1, 1])
lines!(ax, 1:10, linestyle = :dash, label = "Line")
poly!(ax, [(5, 0), (10, 0), (7.5, 5)], label = "Poly")
scatter!(ax, 4:13, label = "Scatter")
Legend(fig[1, 2], ax, "Default")
Legend(fig[1, 3], ax, "rowgap = 10", rowgap = 10)
fig
cbc4cfd

n.表面,表面

默认值为 主题(场景,:colormap)

基于表面的网格元素的默认颜色表

[医]表面颜色

默认值为 自动的

基于表面的网格元素的默认颜色范围。 如果没有设置,这将从表面值派生。

浮出水面

默认值为 (-0.7 .. 0.7, -0.7 .. 0.7,[-0.007*x^3*(1-0.05*y^2)对于x=-5:5,y=-5:5])

用于基于曲面的网格单元的默认(x,y,z)数据。 对于3D元素,摄像机位于(1,1,1)处,朝向(0,0,0),z向上。

表面价值

默认值为 自动的

用于基于表面的网格元素的默认值/颜色。 这些需要匹配zs的大小。 如果不设置,将使用z值。

告诉我们

默认值为 自动的

控制父布局是否可以调整到此元素的高度

告诉宽度

默认值为 自动的

控制父布局是否可以调整到此元素的宽度

[医]针状色

默认值为 @继承:textcolor:黑色

图例标题的颜色

蒂特尔丰特

默认值为 :粗体

图例组标题的字体系列。

[医]滴定管

默认值为 8

每个组标题与其组之间的差距。

[医]铁甲

默认值为 :中心

图例组标题的水平对齐方式。

[医]定位

默认值为 :顶部

组标题相对于其组的位置。 可以是 :顶部:左. </无翻译>

using CairoMakie
fig = Figure()
ax = Axis(fig[1, 1])
lines!(ax, 1:10, linestyle = :dash, label = "Line")
poly!(ax, [(5, 0), (10, 0), (7.5, 5)], label = "Poly")
scatter!(ax, 4:13, label = "Scatter")
Legend(fig[1, 2], ax, "titleposition
= :top", titleposition = :top)
Legend(fig[1, 3], ax, "titleposition
= :left", titleposition = :left)
fig
ec1c01a

[医]针状物

默认值为 @继承:字体大小16.0f0

图例组标题的字体大小。

[医]标价

默认值为 :中心

图例组标题的垂直对齐方式。

[医]可分性

默认值为 真的

控制图例标题是否可见。

n.英勇,英勇

默认值为 :中心

图例在其建议的边界框中的垂直对齐。

阔度

默认值为 汽车()

图例的宽度设置。