【问题标题】:Plotly Dash Multiple plots why I am getting an errorPlotly Dash Multiple 显示我收到错误的原因
【发布时间】:2021-09-11 14:37:05
【问题描述】:

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

Multiple plotly plots on 1 page without subplot

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

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 

【问题讨论】:

  • 关于另一个 SO 问题的不明确答案的问题应该在相应的 cmets 中提出,而不是通过创建新问题来提出。 style=col_style[i]html.Div() 函数的可选参数,可以删除。如果您想要特定的绘图样式,请尝试阅读函数定义及其参数style
  • 如果您不介意,请分享解释样式和 col_style 的链接。我在破折号中搜索但失败

标签: python plotly plotly-dash


【解决方案1】:

以我的评论为基础:

style=col_style[i] 是 Plotly Dash 的 html.Div() function 的可选参数,如果您不想将绘图样式更改为特定的样式(您不必完成这项工作)。

即只需使用

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

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

【讨论】:

  • 谢谢你,我运行了你给的代码。但是没有输出。我看不到任何图像。空白 html 页面即将到来。
  • @HARITO 你是否能够创建一个只有一个情节的 HTML 页面?您尝试过的最后一种至少部分奏效的方法是什么?
  • 我更新了问题。我在工作中使用了其他方法我使用 dcc 绘制每个图表然后我用谷歌搜索并在 SO 中找到了这种方法。但是该代码显示错误。
  • @HARITO 请更新问题,以便我可以复制并粘贴您的代码并且它可以工作。当您使用我提议的更改运行新代码时,还将您的命令行显示的内容发送给我。
  • 更新了问题我已经添加了运行你建议的修改后获得的输出。我得到一个空的 html 文件。在我的代码中,您可以看到我需要绘制 10 个数字[图 0 到图 9] 我想使用循环来减小代码的大小
猜你喜欢
  • 2022-08-14
  • 2019-06-25
  • 2022-07-30
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2022-01-16
相关资源
最近更新 更多