python 做完了dash 怎么运用_python - 如何使用for循环在Dash + Python中显示多个图表? - 堆栈内存溢出...

我正在尝试使用循环来打印图表。 数据在列表中。 这是我的代码当前的外观(在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:我知道我可以在下面的两个图表中进行手动编码,但是我希望将其放在一个循环中,因为将来我可能会在一页上显示两个以上的图表。

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值