我正在尝试使用循环打印图表。数据在列表中。这是我的代码当前的外观(在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)
我该怎么做?
谢谢。
EDIT 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
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)
2ND EDIT 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)
答案 0 :(得分:1)
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)