【问题标题】:Plotly: How does Dash describe data without import plotly or plotly expressPlotly:Dash 如何在没有导入 plotly 或 plotly express 的情况下描述数据
【发布时间】:2020-12-11 02:30:48
【问题描述】:

我正在浏览来自https://dash-gallery.plotly.host/dash-oil-and-gas/ 的 Dash 仪表板示例项目。使用app.py 文件,我没有找到 px 或 go 被导入。相反,数据被描述为dict类型的数据,例如:

data = [
    dict(
        type="scatter",
        mode="markers",
        x=g.index,
        y=g["API_WellNo"] / 2,
        name="All Wells",
        opacity=0,
        hoverinfo="skip",
    ),
    dict(
        type="bar",
        x=g.index,
        y=g["API_WellNo"],
        name="All Wells",
        marker=dict(color=colors),
    ),
]
figure = dict(data=data, layout=layout_count)
return figure

我找不到任何关于如何在没有 import px 或 go 的情况下完成此操作的说明。请告诉我在这种特殊情况下这是如何工作的?

【问题讨论】:

  • 如果我的建议有用,请考虑投票和/或将其标记为已接受的答案。

标签: python plotly trace plotly-dash


【解决方案1】:

必要的导入是通过

import dash_core_components as dcc

您所指的代码部分是此回调的一部分:

@app.callback(
    Output("count_graph", "figure"),
    [
        Input("well_statuses", "value"),
        Input("well_types", "value"),
        Input("year_slider", "value"),
    ],
)
def make_count_figure(well_statuses, well_types, year_slider):

# [...]

    data = [
        dict(
            type="scatter",
            mode="markers",
            x=g.index,
            y=g["API_WellNo"] / 2,
            name="All Wells",
            opacity=0,
            hoverinfo="skip",
        ),

注意Output("count_graph", "figure") 部分。这表示要将figure 输出到包含dcc.Graph 对象和id="count_graph"html.div

html.Div(
    [dcc.Graph(id="count_graph")],
    id="countGraphContainer",
    className="pretty_container",
)

现在,如果您在dcc.Graph 中的Using the Low-Level Interface with Dicts & Lists 部分下查看,您会发现生成图形所需的只是正确定义的字典,如您问题中的示例所示。 gopx 都不需要。

import dash_core_components as dcc
dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
                {'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
            ],
            'layout': {
                'title': 'Dash Data Visualization'
            }
        }
    )

但是您可以在此处使用gopx,因为go 或px 生成的任何图形对象也被构造为字典。

【讨论】:

    猜你喜欢
    • 2016-10-11
    • 2021-03-04
    • 2021-10-21
    • 2016-03-04
    • 1970-01-01
    • 2021-08-11
    • 2021-05-14
    • 1970-01-01
    • 2019-11-04
    相关资源
    最近更新 更多