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

2024-10-06 10:21:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将水印添加到bokeh现有的分类图表中。我试图通过多种方法添加图像,但还没能使其工作。在

我尝试过使用category(字符串)作为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。在

谢谢你的帮助!在


Tags: 图像jsonurlexecuteapplication分类rootcontent
1条回答
网友
1楼 · 发布于 2024-10-06 10:21:11

您可以在Div中使用此解决方案。您可以在Div中放置任意的HTML代码,并使用内联css样式表(bokehv1.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))

结果:

enter image description here

相关问题 更多 >