plotly - 在 Dash for Julia 中添加水平线

标签 plotly julia plotly-dash

我正在从 Julia 的角度尝试使用 Dash 来创建仪表板。我有一个包含以下代码的工作图。

using DataFrames, XLSX, Dates, Dash, DashHtmlComponents, DashCoreComponents, StatsPlots, Indicators

df = DataFrame(XLSX.readtable("file.xlsx", "Price", infer_eltypes=true)...)
    filter!(row -> row.Date >= Date(2018,12,1), df)
    sort!(df)

app  = dash()

app.layout = html_div() do
    html_h1("Price graph"),
    dcc_graph(
        id = "example-graph-1",
        figure = (
            data = [
                (x = df.Date, y = df.Closeprice, type = "line", name = "Closeprice"),
                (x = df.Date, y = sma(df.Closeprice, n=50), type = "line", name = "SMA50"),
                (x = df.Date, y = sma(df.Closeprice, n=200), type = "line", name = "SMA200")
            ],
            layout = (title = "Simple Moving Average", barmode="group")
        )
    )
end

run_server(app, "0.0.0.0", debug=true)

现在我想在该图的 y 轴上的某些点处创建一些水平线。我尝试查看文档,但没有找到任何关于如何创建水平线的好示例。 当我使用 Statsplot 库时,我可以使用 hline 和 vline 创建不同的线,但这似乎在 Dash 中不起作用。任何指导将不胜感激。

最佳答案

您可以向布局添加形状。

简化的可重现示例:

using DataFrames, Dash, DashHtmlComponents, DashCoreComponents

df = DataFrame(Date = [1, 2, 3, 4], Closeprice = [1, 3, 5, 8])

app = dash()

app.layout = html_div() do
    html_h1("Price graph"),
    dcc_graph(
        id = "example-graph-1",
        figure = (
            data = [(x = df.Date, y = df.Closeprice, type = "line", name = "Closeprice")],
            layout = (
                title = "Simple Moving Average",
                barmode = "group",
                shapes = [(
                    type = "line",
                    x0 = 0,
                    x1 = 1,
                    xref = "paper",
                    y0 = 4,
                    y1 = 4,
                    yref = "y",
                    line = (color = "Red", width = 2)
                )],
            ),
        ),
    )
end

run_server(app, "0.0.0.0", debug = true)

结果:

hline

使用

xref = "paper" 以便线条跨越整个图表。

By default, text annotations have xref and yref set to "x" and "y", respectively, meaning that their x/y coordinates are with respect to the axes of the plot. This means that panning the plot will cause the annotations to move. Setting xref and/or yref to "paper" will cause the x and y attributes to be interpreted in paper coordinates.

https://plotly.com/python/text-and-annotations/

以上引用来自 Python 文档,但同样适用于此处。

关于plotly - 在 Dash for Julia 中添加水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68314851/

相关文章:

css - 在 Dash 布局中创建混合宽度和高度的单元格

python - 如何使用按钮触发回调更新?

python - 添加注释文本会产生整数错误消息

julia - 如何获取类型的所有方法

python - Plotly-Dash:如何制作带有交叉表的数据框,仍显示原始列名称?

algorithm - 用于一般可迭代集合的 Julia 查找算法

julia - 找不到 Julia 的 bin 文件夹?

r - 强制绘图 fiddle 图不在零值上显示 fiddle

python - plotly dash 不刷新

Python Plotly Express : How to conditionally fill an area plot?