如何在Bokeh中使用image_url放置图像

问题描述 投票:0回答:1

我有一个图表,在Bokeh中使用日期时间作为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')

我在图表中有几个工作得很好的标签。是否可以使用屏幕单位指定图像位置和大小的方法,与指定标签的位置相同?

python bokeh
1个回答
0
投票

[我会发布我如何使这项工作前进的。我在Bokeh图中使用了以下内容,该图将徽标与一些通用数学运算放在一起,以将数据空间转换为屏幕空间。它无需使用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可以是该模式的任何类型,datetime只是我正在处理的问题。

© www.soinside.com 2019 - 2024. All rights reserved.