python - 如何在 Bokeh 中使用 image_url 放置图像
问题描述
我有一个图表,它在散景中使用日期时间作为 x 轴,使用美元作为 y 轴。我想在绘图区域的左上角放置一个徽标。散景文档在放置图像时似乎特别神秘。此代码有效:
from bokeh.plotting import figure, show
#p = figure(x_range=(0,1200), y_range=(0,600))
p = figure(plot_width=1200, plot_height=600,
sizing_mode = 'scale_width',
toolbar_location='above',
x_axis_label='date',
x_axis_type='datetime',
y_axis_label='value',
)
p.image_url(x=0, y=1, url=["Shrewd_Lines_200.png"], anchor='bottom_left')
show(p)
但是当我将它放入我的主图表中时,数据在日期时间中,我无法显示图像。以下是主图表中代码的关键摘录:
plot = figure(plot_width=1200, plot_height=600,
sizing_mode = 'scale_width',
toolbar_location='above',
tools=tools,
title=plot_dict['chart_title'],
x_axis_label='date',
x_axis_type='datetime',
y_axis_label='value',
)
plot.x_range.end=plot_dict['end_data'] + extend_time
if plot_dict['start_chart'] == 'auto':
plot.x_range.start=plot_dict['start_user_data']
else:
plot.x_range.start = plot_dict['start_chart']
plot.y_range.start=0
plot.y_range.end= extend_y * plot_dict['max_value']
plot.left[0].formatter.use_scientific = False
plot.title.text_font_size = "16pt"
我尝试了各种方法来绘制图像,例如:
plot.image_url(x=0, y=0, url=["Shrewd_Lines_200.png"], anchor='bottom_left')
plot.image_url(x=plot_dict['start_user_data'], y=10000000, url=["Shrewd_Lines_200.png"], anchor='bottom_left')
我在图表中有几个标签效果很好。有没有办法使用屏幕单位指定图像位置和大小,与指定标签位置的方式相同?
解决方案
以为我会发布我如何让这个工作以继续前进。我将以下内容用于我的散景图,将我的徽标与一些通用数学相结合,以将数据空间转换为屏幕空间。它在不使用 numpy 数组或 ColumnDataSource 的情况下执行此操作(两者都不好,但尽量保持简单):
from bokeh.plotting import figure, show
# chart size and ranges need defined for dataspace location
# chart size
chart_width = 900
chart_height = 600
aspect_ratio = chart_width/chart_height
# limits of data ranges
x1 = 300
x2 = 1200
y1 = 0
y2 = 600
plot = figure(
plot_width=chart_width,
plot_height=chart_height,
x_range=(x1, x2),
y_range=(y1, y2),
sizing_mode = 'stretch_both',
x_axis_label='date',
x_axis_type='datetime',
y_axis_label='value')
plot.image_url(url=['my_image.png'], x=(.01*(x2-x1))+x1, y=(.98*(y2-y1))+y1,
w=.35*(x2-x1)/aspect_ratio, h=.1*(y2-y1), anchor="top_left")
show(plot)
请注意 x_axis_type 可以是具有此架构的任何类型,日期时间正是我正在处理的问题。
推荐阅读
- mysql - 我可以用这种方式调整这个 SQL 吗?
- oracle - 如何从 case when 中删除 NULL 然后输出 Oracle SQL Developer
- haskell - 如果索引列表遍历不匹配,则返回“Nothing”
- python-3.x - Django 无法建立与服务器的连接
- angular - 讨论:Angular 可以自动生成模板吗?
- twilio - Twilio 屏幕截图 web
- javascript - JS for 循环无法在 Live Wordpress 网站上运行,尽管它在本地运行
- node.js - 我无法从我的路由文件夹之外的护照访问 req.user。MERN堆栈redux node.js
- css - 网格单元被推到下一行
- oracle - 使用 RAISERROR 触发