我想用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 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_url
与本地文件一起使用,因为出于安全原因,网页无法访问本地文件。我可以想到的两个可行的选择:
bokeh serve
。它将允许您通过static
目录image_url
,请使用image_rgba
。您已经阅读了整个图像,因此可以将其用作数据,而不是用作URL如果无法使image_url
工作,请尝试使用其锚点,并确保url
参数以目录名称开头。我可以通过创建一个名为test_app
的目录来显示图像:
test_app
├── main.py
└── static
└── tree.png
仅main.py
具有
from bokeh.io import curdoc
from bokeh.plotting import figure
p = figure()
p.image_url(url=['/test_app/static/tree.png'],
x=[0], y=[0], w=[1], h=[1], anchor="bottom_left")
curdoc().add_root(p)
它应以bokeh serve test_app
身份运行。