如何使用R制作带有弹出图片的交互式散点图and/or Python

How to make interactive scatter plot with pop-up picture by using R and/or Python

我想制作交互式散点图。

具体来说,当我单击或将鼠标指针放在一个点上时,应该会显示相应的图片。

数据结构由x、y和文件路径(图片)组成。

我尝试了以下代码,但没有成功。

library(plotly);library(png)

tmp=data.frame(x=c(1,2,3),y=c(1,2,3),
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代码就可以了。

例如 has been asked before. Answer in this question and other sources such as this suggest that there is no easy way to do this just with plotly. However, you can use it with shiny to create the custom hover images, see 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, 2, 3],
    imgs=[
        'C:\Users\username\Downloads\pic.png',
        '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 可能是您喜欢使用的大脑图像。