<分区>
假设我在 plotly 中有这个散点图:
import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.show()
但是我想在这些特定位置添加不同的图像而不是点。像这样:
plotly 可以吗?我知道你可以add Images但我不知道如何传递应该显示图像的位置的确切坐标。
感谢您的宝贵时间!谢谢。
<分区>
假设我在 plotly 中有这个散点图:
import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.show()
但是我想在这些特定位置添加不同的图像而不是点。像这样:
plotly 可以吗?我知道你可以add Images但我不知道如何传递应该显示图像的位置的确切坐标。
感谢您的宝贵时间!谢谢。
最佳答案
import plotly.express as px
import kaggle.cli
import sys, requests
from pathlib import Path
from zipfile import ZipFile
import urllib
from PIL import Image
# fmt: off
# download some images to demonstrate
url = "https://www.kaggle.com/anzhemeng/nfl-team-logos"
sys.argv = [sys.argv[0]] + f"datasets download {urllib.parse.urlparse(url).path[1:]}".split(" ")
kaggle.cli.main()
zfile = ZipFile(f'{urllib.parse.urlparse(url).path.split("/")[-1]}.zip')
# fmt: on
zfile.extractall("nfl-logos")
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
# question did not use a dataframe, so will use x & y from the figure trace
# just a selection of images, used NFL images given don't have emojis
for x,y, png in zip(fig.data[0].x, fig.data[0].y, Path.cwd().joinpath("nfl-logos").glob("*.png")):
fig.add_layout_image(
x=x,
y=y,
source=Image.open(png),
xref="x",
yref="y",
sizex=2,
sizey=2,
xanchor="center",
yanchor="middle",
)
fig
关于python - 如何在 plotly 散点图中添加图像而不是点(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70053158/
相关文章:
image - 如何添加允许查看图像背景的边框 ("triangled")?
java - 显示来自 PostgreSQL 数据库 bytea 的图像
html - CSS 使用 List-Style-Image 调整文本在 HTML 列表中的位置
python - 如何在 Plotly 中作为查看器启用和禁用对数刻度?
python - 创建新的 TemporaryDirectory 对象不会创建目录
python - 如何在 Matplotlib 中填充任意闭合区域?
python - 简单的python正则表达式找不到字(在空格字符内)
python - 如何使用带有日期的plotly graph_objects scatter
python - 如何解决错误 : TypeError: invalid Figure property: Layout Layout Bad property path: Layout ^^^^^^