【问题标题】:Gadfly (Julia): how to add text annotation to a plot?Gadfly (Julia):如何在绘图中添加文本注释?
【发布时间】:2018-06-19 09:54:26
【问题描述】:

在 Stata 中,我可以在给定坐标处将文本添加到绘图中,例如:

clear

set obs 10

gen x = rnormal()
gen y = rnormal()

twoway scatter y x, text(0.8 0.8 "Some text")

结果是坐标 (0.8, 0.8) 处的“一些文本”:

我想用 Gadfly 在 Julia 中添加一个类似的注释。我发现 Guide.annotation 可以添加图形图层,但我不知道如何将其应用于文本而不是形状。文档在顶部提到:

用任意Compose 图形覆盖绘图。

Compose链接显示的是中文网站。

如何使用 Gadfly 添加文本标签(或标题或注释)?

【问题讨论】:

    标签: plot julia gadfly


    【解决方案1】:

    您可以在julia 中查看guide on Compose。在您链接中的示例中,他们使用Circle,但您也可以轻松使用:

    text(x, y, value)
    

    或使用链接的示例代码:

    Pkg.add.(["Gadfly", "Compose"])
    using Gadfly, Compose;
    plot(x = rand(10),
         y = rand(10),
         Guide.annotation(compose(context(), text(0.8, 0.8, "Some text"))))
    

    在我提供的链接中,他们重定向到综合列表的源文件:

    这些是内置表单的基本构造函数 - 有关更多构造函数,请参见 src/form.jl。

    • 多边形(点)

    • 矩形(x0,y0,宽度,高度)

    • 圆(x, y, r)

    • 椭圆(x, y, x_radius, y_radius)

    • 文本(x,y,值)

    • 线(点)

    • 曲线(anchor0,ctrl0,ctrl1,anchor1)

    • 位图(mime, data, x0, y0, width, height)

    【讨论】:

    • 是的,这适用于正常规模:using Gadfly, Compose; plot(x = rand(10), y = rand(10), Guide.annotation(compose(context(), text(0.8, 0.8, "Some text"))))。但它在对数范围内失败:using Gadfly, Compose; plot(x = rand(10), y = rand(10), Scale.y_log10, Guide.annotation(compose(context(), text(0.8, 0.8, "Some text"))))。它看起来像一个错误。你有解决方法吗?
    • @mmorin 你能指定什么是错误吗?如果它太复杂而无法解决 t 可能值得接受这个答案并就此提出另一个关于 SO 的问题。
    • 我发布了这个on GitHub,错误是“Guide.annotation 不会自动调整为 Scale.y_log10”,因此解决方案是“在 text() 中使用 log10(0.8)”。是的,我接受了答案。
    • @mmorin 啊,我看到了这个错误。太棒了,你这么快就找到了解决方法。
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 2021-07-07
    相关资源
    最近更新 更多