亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 for 循環在 Dash + Python 中顯示多個圖表?

如何使用 for 循環在 Dash + Python 中顯示多個圖表?

翻閱古今 2021-11-30 15:39:28
我正在嘗試使用循環打印圖表。數據在一個列表中。這是我的代碼當前的外觀(我在 for 循環中收到語法錯誤):import dashimport dash_core_components as dccimport dash_html_components as htmlimport pandas as pdimport plotly.graph_objs as godfs = [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 = 0app.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)我希望它看起來像這樣: 我該怎么做呢?
查看完整描述

1 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

該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)


查看完整回答
反對 回復 2021-11-30
  • 1 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號