问题描述
我想做一个交互式散点图。
具体来说,当我点击或将鼠标指针放在一个点上时,相应的图片应该是可视化的。
我尝试了以下代码,但没有奏效。
library(plotly);library(png)
tmp=data.frame(x=c(1,2,3),y=c(1,file_path=c('C:/Users/osj118/Pictures/brain_illustrator.png','C:/Users/osj118/Pictures/Cap 2021-05-17 12-32-24-509.png','C:/Users/osj118/Pictures/brain_illustrator.png')
)
tmp %>% plot_ly() %>% add_trace(x=~x,y=~y,z=readpnG(~file_path))
Error in path.expand(source) : invalid 'path' argument
下图是我想做的。
我可以使用python代码。
解决方法
之前曾询问过 related question。在这个问题和其他来源(例如 this)中的回答表明,没有简单的方法可以仅使用 plotly。但是,您可以使用它与 Shiny 一起创建自定义悬停图像,例如参见 this。
如果您愿意按照问题中的说明使用 python,则有一种更简单的方法可以执行此操作,如下所示 (Source):
from bokeh.plotting import ColumnDataSource,figure,output_file,show
output_file("toolbar.html")
source = ColumnDataSource(data=dict(
x=[1,2,3],y=[1,imgs=[
'C:\\Users\\username\\Downloads\\pic.png','C:\\Users\\username\\Downloads\\pic.png',],))
TOOLTIPS = """
<div>
<div>
<img
src="@imgs" height="200" alt="@imgs" width="200"
style="float: left; margin: 0px 15px 15px 0px;"
border="2"
></img>
</div>
</div>
"""
p = figure(plot_width=400,plot_height=400,tooltips=TOOLTIPS,title="Mouse over the dots")
p.circle('x','y',size=20,source=source)
show(p)
这里的 pic.png 可能是您喜欢使用的大脑图像。