首页 > 解决方案 > Python plotly 徽标从图表中消失

问题描述

我必须创建一个显示一个数据表并在顶角有两个徽标的简单报告。下面的代码在以前的项目中工作,但现在我在新计算机上将它重用于新项目,它不会显示徽标。

我没有收到错误消息。相同版本的 plotly 和 python "plotly==4.6.0" "Python 3.6.1"

请注意,唯一改变的是数据表中显示的数据。

import plotly.graph_objects as go
import pandas as pd



traces = go.Table(
    header=dict(values=list(df.columns),
            align='left'),
    cells=dict(
        values=df.T.values.tolist(),
        align='left'))



layout = go.Layout(
                title='Report <br>  {}'.format( report_date),
                title_x=0.5,
                paper_bgcolor='#FFFFFF',
                margin = {'t':100, 'b':40, 'r':40, 'l':40}
                ,images=[
                    dict(
                        source='assets\\MiniLogo.png',
                        xref='paper',yref='paper',
                        x=1,y=1.05,
                        sizex=0.2, sizey=0.2,
                        xanchor="right", yanchor="bottom"), 
                    dict(
                        source='assets\\Titlelogo.png',
                        xref='paper',yref='paper',
                        x=0,y=1.05,
                        sizex=0.2, sizey=0.2,
                        xanchor="left", yanchor="bottom")

                    ]

                )
fig = go.Figure(
        data=traces
        ,layout=layout)
fig.show()

标签: pythonplotly

解决方案


我认为问题出source在您的layout. 我已将您的代码与此图像 URL而不是相对路径一起使用,它运行良好,这是一个屏幕截图,知道我使用了一个简单的表格作为我的df

在此处输入图像描述

在我看来,你有两种选择来克服这个问题:

  • 将这些图像上传到云服务并使用它们的 URL。

  • 或者根据这个Plolty community thread,您可以使用Pillow.Image类从本地机器读取图像。您可以通过运行pip install pillow和修改代码来轻松安装它,如下所示:

    from PIL import Image
    
    layout= go.Layout(images= [dict(
                      source= Image.open('assets\\MiniLogo.png'),
                      ...)])
    

推荐阅读