有没有办法将图像放置在使用分类数据的散景图中?

Is there a way to place an image inside of a bokeh plot that uses categorical data?

我正在尝试将水印添加到散景中的现有分类图表。我已经尝试通过多种方法添加图像,但还没有成功。

我试过使用类别(字符串)作为 x 位置值。这导致 WebDriverException:

{"errorMessage":"undefined is not an object (evaluating 'document.getElementsByClassName('bk-root')[0].children[0].getBoundingClientRect')","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Content-Length":"171","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:52857","User-Agent":"selenium/3.141.0 (python windows)"},"httpVersion":"1.1","method":"POST","post":"{\"script\": \"\nreturn document.getElementsByClassName('bk-root')[0].children[0].getBoundingClientRect()\n\", \"args\": [], \"sessionId\": \"308a7240-6123-11e9-a877-6d9cf99e3c61\"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/308a7240-6123-11e9-a877-6d9cf99e3c61/execute"}}

我也试过将类别作为列表中的位置传递 (category[5])。这不会出错,但不会在绘图上放置图像。

# Add watermark
p.image_url(url=[r'static/images/image.png'], x='Mismatch', y=500, w=100, h=100,
                anchor = 'center',
                global_alpha = 0.2)

预期结果是图像将出现在我的分类图上所需的位置。

实际结果是没有图像出现,或者出现webdriverexception。

感谢您的帮助!

您可以将此解决方法与 Div 结合使用。您可以在 Div 中放置任意 HTML 代码,并使用内联 css 样式表(Bokeh v1.1.0)将此 Div 放置在屏幕上的任何位置。

from bokeh.plotting import figure, show
from bokeh.models import CustomJS, Div, Row

p1 = figure(plot_width = 300, plot_height = 300, x_range = (0, 10), y_range = (0, 10), title = "Doggy as background")
p1.line([1, 2, 3, 5, 6, 7, 8, 9, 10], [4, 5, 6, 1, 2, 3, 7, 8, 9, 0], line_width = 5)
url = "https://cdn3.iconfinder.com/data/icons/line/36/dog_head-512.png"
d1 = Div(text = '<div style="position: absolute; left:-300px; top:10px"><img src=' + url + ' style="width:280px; height:280px; opacity: 0.3"></div>')

show(Row(p1, d1))

结果: