python - 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]
有人可以告诉我如何解决这个问题
解决方案
最简单的方法是指定使用 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
)
)
推荐阅读
- python - 使用带有 IAM 数据库身份验证的 python 连接到 AWS RDS
- ag-grid - rowModelType = 'infinite' 具有最新的 ag-grid 版本,当我重置 rowHeight 并重置数据源时,行加载异常
- regex - Powershell RegEx 不允许字符类
- woocommerce - WooCommerce - 在最后一个类别中获取(直接)父类别
- c# - USER32 SetWindowPos 隐藏光标
- docker - Docker-Compose(如 Vagrant)的私有可寻址 IP
- angular - 使用动态导入延迟加载组件时的编译器警告
- groovy - 如何在groovy语言中删除字符串的第一个和最后一个特殊字符
- asp.net-core - Serilog - 如何停止请求生命周期日志并仅启用自定义信息日志
- reactjs - 代码拆分 ReactJS 组件以分离 API 调用函数在导入提取的函数时测试失败