Plotly:Dash如何在不导入Plotly或Plotly express的情况下描述数据



我正在从https://dash-gallery.plotly.host/dash-oil-and-gas/.对于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

我找不到任何关于如何在没有导入px或go的情况下完成这项工作的说明。你能告诉我在这种特殊情况下是如何工作的吗?

必要的导入通过完成

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的html.div。带id="count_graph":的图形对象

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

现在,如果您看一下dcc中的Using the Low-Level Interface with Dicts & Lists部分。图形,你会发现生成图形所需要的只是一个正确定义的dict,就像你问题中的例子一样。既不需要go也不需要px。

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'
}
}
)

但是,如果您愿意的话,可以使用go或px,因为go或px生成的任何图形对象也都是dicts结构的。

最新更新