Я пытаюсь распечатать графики, используя цикл. Данные в списке. Вот как мой код выглядит в настоящее время (я получаю синтаксическую ошибку в цикле 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)
2 РЕДАКТИРОВАНИЕ 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)