首页 > 解决方案 > Plotly Dash Multiple 显示我收到错误的原因

问题描述

我正在尝试在破折号中绘制多个图表。我从 SO.Plese 获得了一个代码。请参阅下面的链接

1 页上的多个情节情节,没有子情节

当我运行此处选择作为答案的相同代码时,出现错误。

NameError:名称“col_style”未定义

请看下面的代码。我可以知道我错了吗

import plotly.offline as pyo
import plotly.graph_objs as go
import plotly as py

import dash
import dash_core_components as dcc
import dash_html_components as html

fig1 = go.Scatter(y=[1,2,3])
fig2 = go.Scatter(y=[3,2,1])
plots = [fig1, fig2]

app = dash.Dash()
layout = html.Div(
        [html.Div(plots[i], style=col_style[i]) for i in range(len(plots))],
        style = {'margin-right': '0px'}
    )

app.layout = layout
app.run_server()

我的代码目前用于绘制 10 张图像

app = dash.Dash()
app.layout = html.Div([
    html.Div(html.H1(children="TEST SUIT1"),style={'textAlign': 'center','color': '#5742f5', 'fontSize': 20}),
   
    dcc.Graph(
        id = 'graph1',
        figure=fig),
    
    dcc.Graph(
        id = 'graph2',
        figure=fig1),
    
    dcc.Graph(
        id = 'graph3',
        figure=fig2),
    
    dcc.Graph(
        id = 'graph4',
        figure=fig3),
    
     dcc.Graph(
        id = 'graph5',
        figure=fig4),
    
    dcc.Graph(
        id = 'graph6',
        figure=fig5),
    
    dcc.Graph(
        id = 'graph7',
        figure=fig6),
    
    dcc.Graph(
        id = 'graph8',
        figure=fig7),
    
    
      dcc.Graph(
        id = 'graph9',
        figure=fig8)
])

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

进行 HLZL 建议的更改后得到的输出。我得到一个没有任何图形的空 html 文件。

Dash is running on http://127.0.0.1:8050/

     * Serving Flask app "__main__" (lazy loading)
     * Environment: production
       WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: off
     * Running on http://127.0.0.1:8050/ (Press CTRL+C to quit)
    127.0.0.1 - - [29/Jun/2021 20:11:13] "GET / HTTP/1.1" 200 -
    127.0.0.1 - - [29/Jun/2021 20:11:13] "GET /_dash-layout HTTP/1.1" 200 -
    127.0.0.1 - - [29/Jun/2021 20:11:13] "GET /_dash-dependencies HTTP/1.1" 200 

标签: pythonplotlyplotly-dash

解决方案


以我的评论为基础:

style=col_style[i]是 Plotly Dashhtml.Div() 函数的可选参数,如果您不想将绘图的样式更改为特定的东西(您不必这样做),可以将其删除。

即简单地使用

layout = html.Div(
        [html.Div(plots[i]) for i in range(len(plots))],
        style = {'margin-right': '0px'}
    )

该变量col_style[i]是 Python 字典和未进一步定义的原始作者的自定义变量。如果您想定义自定义样式,请开始阅读 Plotly Dash HTML 组件的工作原理。该style论点使用 CSS 作为基础。


推荐阅读