【问题标题】:Dash doesn't show figure created with px.imshowDash 不显示使用 px.imshow 创建的图形
【发布时间】:2022-01-03 00:56:59
【问题描述】:

如标题所示,Dash 不显示使用 px.imshow 创建的图形(图片)。下面是我的代码。

import plotly.graph_objects as go # or plotly.express as px
import plotly.express as px
import numpy as np
import dash
import dash_core_components as dcc
import dash_html_components as html

img_rgb = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
                    [[0, 255, 0], [0, 0, 255], [255, 0, 0]]
                   ], dtype=np.uint8)
fig = px.imshow(img_rgb)

app = dash.Dash()

app.layout = html.Div([
    dcc.Graph(figure=fig)
])
app.run_server(debug=True)  

我用过这个documentation page。在最底部,它说:

在此页面中您看到 fig.show() 的任何地方,您都可以在 Dash 应用程序中显示相同的图形,方法是将其传递给内置 dash_core_components 包中的 Graph 组件的图形参数,如下所示:

import plotly.graph_objects as go # or plotly.express as px
fig = go.Figure() # or any Plotly Express function e.g. px.bar(...)
# fig.add_trace( ... )
# fig.update_layout( ... )

import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()
app.layout = html.Div([
    dcc.Graph(figure=fig)
])

app.run_server(debug=True, use_reloader=False)  # Turn off reloader if inside Jupyter

那我做错了什么?

【问题讨论】:

  • 当我运行您的代码并将fig 传递给破折号app 对象时,该图为我正常呈现。你升级到最新版本的 plotly 和 dash 了吗?您是否有机会使用 jupyter 笔记本?
  • 谢谢,确实是版本的问题。这次我是通过 conda 安装包,我以为它也是像 pip 一样下载最新版本。

标签: python plotly plotly-dash


【解决方案1】:

为了让它运行,我唯一改变的是:

from dash import dcc
from dash import html

而不是

import dash_core_components as dcc
import dash_html_components as html

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 2019-06-25
    • 2022-07-30
    • 1970-01-01
    • 2021-11-14
    • 2021-12-10
    • 2020-02-19
    • 2022-01-06
    相关资源
    最近更新 更多