首页 > 解决方案 > Dash-Plolty 未在图中显示图像

问题描述

我正在尝试将图像放在我的身材上,就像水印一样,我正在关注文档。它适用于“Vox”示例。但是,当我尝试将本地图像放入图中时,它们不会出现。

这是我的代码

import plotly.express as px
import requests
import pandas as pd 


response = requests.get("https://api.covalenthq.com/v1/1/address/0x343A53A1E8b17beDd15F47a28195Bc8C120d4443/portfolio_v2/?format=format%3Dcsv&key=ckey_57eeb470248541708eeaf028c9d").json()['items']
data=pd.json_normalize(response,record_path=['holdings'],meta=['contract_ticker_symbol','contract_name',"contract_address"])
data['timestamp']=pd.to_datetime(data['timestamp']).dt.strftime('%D')



#colors = {
  #  'background': 'black', #Sets plot background color black
   # 'text': '#FFFFFF' #Sets plot text color white
#}
fig = px.line(data, x="timestamp", y="close.quote", color="contract_name",color_discrete_sequence=["#ff4c8b", "#00d8d5",'#f7f7f7'], line_group="contract_ticker_symbol",labels={    #Changes colum names
                    "contract_name":'Contract Name', 
                     "timestamp": "Date",
                     "close.quote": "USD Value",
                     "contract_ticker_symbol": "Ticker"
                 }, title='Asset Value Over Time', hover_name="contract_ticker_symbol")

fig.add_layout_image(
    dict(
        source="vox.png",
        xref="paper", yref="paper",
        x=0.5, y=0.24,
        sizex=0.5, sizey=0.6,
        xanchor="center", yanchor="bottom"
    )
)


fig.add_layout_image(
            dict(
                source="aa_footer.svg",
                xref="paper", yref="paper",
                x=0.7, y=(-0.20),
                sizex=1.7, sizey=.8,
                xanchor="center", yanchor="bottom"
            )
        )
fig.update_layout(plot_bgcolor='black', paper_bgcolor='black',font_color='#FFFFFF')

# update layout properties
fig.update_layout(
    margin=dict(r=20, l=300, b=75, t=125),
    title=("Asset Valuation Overtime<br>" +
           "<i>Assets in Ethereum Blockchain</i>"),
)
fig.update_xaxes(showgrid=False) #hide vertical gridlines

fig.show()

我尝试将我的图像放入“资产”文件夹和外部,并将它们上传到 imgBB。仍然没有回应

这是我得到的数字:

[![在此处输入图像描述][2]][2]

有人可以告诉我如何解决这个问题

标签: pythonplotlyplotly-dash

解决方案


最简单的方法是指定使用 PILLOW 库作为源获得的数据。官方参考描述可以在这里找到。

from PIL import Image # new import

img = Image.open('./data/vox.png') # image path
# defined to source
fig.add_layout_image(
    dict(
        source=img,
        xref="paper", yref="paper",
        x=0.5, y=0.24,
        sizex=0.5, sizey=0.6,
        xanchor="center",
        yanchor="bottom",
        opacity=0.5
    )
)

在此处输入图像描述


推荐阅读