python - 如何使用 for 循环在 Dash + Python 中显示多个图表?

标签 python plotly-dash

我正在尝试使用循环打印图表。数据在一个列表中。这是我的代码当前的样子(我在 for 循环中收到语法错误):

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go

dfs = [pd.DataFrame({"xaxis":["thing","otherthing","anotherthing"],"yaxis":[64,14,62]}),pd.DataFrame({"xaxis":["newthing","newotherthing","newanotherthing"],"yaxis":[344,554,112]})]
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

g = 0

app.layout = html.Div(children=[
    for df in dfs:
        dcc.Graph(id='example-graph'+str(g),figure={'data': [go.Bar(x=df['xaxis'],y=df[("yaxis")],name="yaxis")]})
    ]
    g = g + 1)

if __name__ == '__main__':
    app.run_server(debug=True)

我希望它看起来像这样:

我该怎么做呢?

提前致谢。

编辑 08/03/19:我知道我可以在下面的两个图表中手动编码,但我希望将其放入循环中,因为将来我可能会在一页上显示 2 个以上的图表。
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go

df1 = pd.DataFrame({"xaxis":["thing","otherthing","anotherthing"],"yaxis":[64,14,62]})
df2 = pd.DataFrame({"xaxis":["newthing","newotherthing","newanotherthing"],"yaxis":[344,554,112]})
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

g = 0
j = 1

app.layout = html.Div(children=[

    dcc.Graph(id='example-graph'+str(g),figure={'data': [go.Bar(x=df1['xaxis'],y=df1[("yaxis")],name="yaxis")]}),
    dcc.Graph(id='example-graph' + str(j), figure={'data': [go.Bar(x=df2['xaxis'], y=df2[("yaxis")], name="yaxis")]})
])

if __name__ == '__main__':
    app.run_server(debug=True)

第二次编辑 08/03/19:我的最终工作代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go

dfs = [pd.DataFrame({"xaxis":["thing","otherthing","anotherthing"],"yaxis":[64,14,62]}),pd.DataFrame({"xaxis":["newthing","newotherthing","newanotherthing"],"yaxis":[344,554,112]})]
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

i = 0
output = []
#here you can define your logic on how many times you want to loop
for df in dfs:
     output.append(dcc.Graph(id='example-graph'+str(i),figure={'data': [go.Bar(x=df['xaxis'],y=df[("yaxis")],name="yaxis")]}))
     i = i + 1


app.layout = html.Div(children=output)

if __name__ == '__main__':
    app.run_server(debug=True)

最佳答案

children属性基本上是一个列表,您可以先在通用循环中生成列表,然后将其添加到 Div 中。

这是工作片段,

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go

df1 = pd.DataFrame({"xaxis":["thing","otherthing","anotherthing"],"yaxis":[64,14,62]})
df2 = pd.DataFrame({"xaxis":["newthing","newotherthing","newanotherthing"],"yaxis":[344,554,112]})
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)


output = []
#here you can define your logic on how many times you want to loop
for i in range(0,2): 
     output.append(dcc.Graph(id='example-graph'+str(i),figure={'data': [go.Bar(x=df1['xaxis'],y=df1[("yaxis")],name="yaxis")]}))


app.layout = html.Div(children=output)

if __name__ == '__main__':
    app.run_server(debug=True)

关于python - 如何使用 for 循环在 Dash + Python 中显示多个图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55062551/

相关文章:

python - 如何根据 Python 中的用户输入返回电子表格中的单元格值?

python - 调用异常 : GraphViz's executables not found (Python)

php - 如何将数组从 python 脚本传递给 php?

Python BigQuery API : how to get data asynchronously?

python - 短划线范围 slider ,两侧都有输入

python - 在 dash 中,选择单选按钮时如何使用回调来更新图表?

python - 如何在 Dash 中正确使用 DatePickerRange?我不断收到类型错误

Python、破折号 : how to add a KPI Card component to a dashboard

python - 尝试在 mac 10.7 上安装 pip 时出错

python - Plotly-Dash:如何提高绘图对 slider 更改的响应能力