Python 如何在背景中使用图像创建Bokeh图?
我想用Bokeh创建一个圆形图,背景为图像,请参见下面的代码。我不明白为什么图像不显示。我的代码中是否有错误,或者我使用的方法不正确Python 如何在背景中使用图像创建Bokeh图?,python,image,plot,bokeh,Python,Image,Plot,Bokeh,我想用Bokeh创建一个圆形图,背景为图像,请参见下面的代码。我不明白为什么图像不显示。我的代码中是否有错误,或者我使用的方法不正确 import pandas as pd import cv2 from bokeh.models import ImageURL, ColumnDataSource from bokeh.plotting import figure from bokeh.models.ranges import DataRange1d from bokeh.io import
import pandas as pd
import cv2
from bokeh.models import ImageURL, ColumnDataSource
from bokeh.plotting import figure
from bokeh.models.ranges import DataRange1d
from bokeh.io import show
df = pd.DataFrame({'Time': [2586, 2836, 2986, 3269, 3702],
'X': [120, 210, 80, 98, 40],
'Y': [230, 40, 33, 122, 10],
'User': ['u1', 'u1', 'u2', 'u2', 'u2']})
source = ColumnDataSource(data=dict(time=df['Time'], x=df['X'], y=df['Y'], user=df['User']))
#read image
img_url = 'tree.jpg'
image=cv2.imread(img_url)
sx = image.shape[1]
sy = image.shape[0]
x_range = DataRange1d(start = 0, end = sx, bounds = (0, sx), range_padding = 5, range_padding_units = 'percent')
y_range = DataRange1d(start = sy, end = 0, bounds = (0, sy), range_padding = 5, range_padding_units = 'percent')
pw = 600
ph = int(pw * sy / sx)
# Create the figure: p
p = figure(x_axis_label='X', y_axis_label='Y', plot_width = pw, plot_height = ph,
x_range=x_range, y_range=y_range, match_aspect=True)
p.image_url(url=[img_url], x = 0, y = 0, w = sx, h = sy, anchor="top_right")
p.circle(x='x', y='y', source=source, size=10, color='blue', fill_color='white', alpha=0.8)
show(p)
形象
img_url
需要是结果页面能够访问的正确url。文件的本地文件系统路径不是URL。不幸的是,如果不使用某种web服务器,就不能将image\u url
与本地文件一起使用,因为出于安全原因,网页无法访问本地文件。我可以想到两个可行的选择:
- 与基于目录的应用程序一起使用
。它将允许您通过bokeh serve
目录提供任意文件静态
- 不要使用
,使用image\u url
。您已经读取了整个图像,因此可以将其作为数据而不是URLimage\u rgba
image\u url
正常工作,请尝试使用其锚,并确保url
参数以目录名称开头。我可以通过创建名为test\u app
的目录来显示图像:
test_app
├── main.py
└── static
└── tree.png
其中main.py
只有
从bokeh.io导入curdoc
从bokeh.plotting导入图形
p=图()
p、 image_url(url=['/test_app/static/tree.png'],
x=[0],y=[0],w=[1],h=[1],anchor=“左下”)
curdoc().add_root(p)
它应该作为bokeh serve test_应用程序运行
img_url
需要是结果页面能够访问的正确url。文件的本地文件系统路径不是URL。不幸的是,如果不使用某种web服务器,就不能将image\u url
与本地文件一起使用,因为出于安全原因,网页无法访问本地文件。我可以想到两个可行的选择:
- 与基于目录的应用程序一起使用
。它将允许您通过bokeh serve
目录提供任意文件静态
- 不要使用
,使用image\u url
。您已经读取了整个图像,因此可以将其作为数据而不是URLimage\u rgba
image\u url
正常工作,请尝试使用其锚,并确保url
参数以目录名称开头。我可以通过创建名为test\u app
的目录来显示图像:
test_app
├── main.py
└── static
└── tree.png
其中main.py
只有
从bokeh.io导入curdoc
从bokeh.plotting导入图形
p=图()
p、 image_url(url=['/test_app/static/tree.png'],
x=[0],y=[0],w=[1],h=[1],anchor=“左下”)
curdoc().add_root(p)
它应该作为bokeh serve测试应用程序运行我使用bokeh serve,但它仍然不起作用。该应用程序还包括一个选择小部件和3个其他滑块,工作正常。你认为这会导致图像打印的问题吗?我更新了我的答案,给你一些可以使用的东西。谢谢你,尤金。我按照你的建议构建了这个应用程序结构。现在图像url可以工作了,并且完全不会干扰过滤器小部件。我使用bokeh服务,但它仍然不能工作。该应用程序还包括一个选择小部件和3个其他滑块,工作正常。你认为这会导致图像打印的问题吗?我更新了我的答案,给你一些可以使用的东西。谢谢你,尤金。我按照你的建议构建了这个应用程序结构。现在,image_url正在工作,并且完全不会干扰过滤器小部件。