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

2024-04-19 19:31:36 发布

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

我有一张图表,x轴使用datetime,y轴使用美元,Bokeh。我想在绘图区域的左上角放置一个徽标。Bokeh文档在放置图像时显得特别神秘。此代码适用于:

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)

但是当我把它放在我的主图表中,那里的数据是datetime,我无法得到一个图像来显示。以下是主要图表中代码的关键摘录:

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')

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


Tags: 图像imageurldatetimeplotvalue图表range
1条回答
网友
1楼 · 发布于 2024-04-19 19:31:36

我想我会发布我是如何工作的,以便继续前进。我使用下面的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_类型可以是这个模式下的任何类型,datetime正是我要处理的问题

相关问题 更多 >