【问题标题】:create background image for a plot using plotly/dash使用 plotly/dash 为绘图创建背景图像
【发布时间】:2020-12-26 05:37:04
【问题描述】:

我正在尝试使用背景图像为 plotly/dash 创建一个绘图。绘图已创建,但背景图像未创建。文件和图片在同一目录下。

我错过了什么吗?

import base64
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objects as go

IMAGE_FILENAME1 = 'link.png'
image1 = base64.b64encode(open(IMAGE_FILENAME1, 'rb').read())


fig = go.Figure(
    data=[go.Bar(y=[2, 1, 3])],
    layout_title_text="Native Plotly rendering in Dash"
)

fig.update_layout( images= [dict(
                    #source="KU.png",                    
                    source='data:image/png;base64,{}'.format(image1.decode()),
                    #xref="container",
                    #yref="container",
                    layer="below")])


app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Graph(id="graph", figure=fig),
])

app.run_server(debug=True)

获取无花果的属性:

fig
Figure({
    'data': [{'type': 'bar', 'y': [2, 1, 3]}],
    'layout': {'images': [{'layer': 'below',
                           'source': ('data:image/png;base64,iVBORw0K' ... 'Q7B0QA13afSQHVAAAAAElFTkSuQmCC')}],
               'template': '...',
               'title': {'text': 'Native Plotly rendering in Dash'}}

【问题讨论】:

    标签: python plotly-dash


    【解决方案1】:

    我的建议可能无法解决问题,因为尚不清楚图像未显示哪种图形。我试图用你的代码显示我拥有的图像。 我修改的唯一代码是以下一行。

    import base64
    import dash
    import dash_core_components as dcc
    import dash_html_components as html
    import plotly.graph_objects as go
    from jupyter_dash import JupyterDash 
    
    IMAGE_FILENAME1 = './data/plotly_graphing_libraries_1.png'
    image1 = base64.b64encode(open(IMAGE_FILENAME1, 'rb').read())
    
    app = JupyterDash(__name__)
    # app = dash.Dash(__name__)
    
    fig = go.Figure(data=[go.Bar(x=[0,1,2], y=[2, 1, 3])], layout_title_text="Native Plotly rendering in Dash")
    
    fig.add_layout_image(dict(source='data:image/png;base64,{}'.format(image1.decode()),
                              xref="x",
                              yref="y",
                              x=0, y=3,
                              sizex=2,
                              sizey=2, 
                              sizing="stretch",
                              opacity=0.5,
                              layer="below"))
    
    fig.update_layout(template="plotly_white")
    
    app.layout = html.Div(children=[
        dcc.Graph(id="graph", figure=fig),
    ])
    
    app.run_server(mode='inline')
    

    【讨论】:

    • 如何与 dash 集成?
    • 我在 jupyterlab 上使用 Dash 检查了我的环境。我已经更新了代码。
    猜你喜欢
    • 1970-01-01
    • 2019-01-13
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2022-07-30
    • 2021-01-23
    • 2013-04-10
    相关资源
    最近更新 更多