Bokeh标签字体大小响应于数字大小

2024-03-29 10:15:08 发布

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

我正在添加一个标签作为图形的注释。我可以预先设置标签的字体大小。但是,当调整浏览器的大小时,只有图形的大小是responsive,标签的字体大小是un-responsive

fig = figure(x_axis_type='datetime', y_axis_label=labels[i],
             toolbar_location=None, active_drag=None, 
             active_scroll=None)
fig.line(x='time', y='data', source=source, line_color='red')
annotation = Label(x=10, y=10, text='text', text_font_size='60px', text_color='white', x_units='screen', y_units='screen', background_fill_color=None))

我试图使用图形的高度来调整字体大小,但这不起作用。有没有办法达到这个目的?谢谢你的任何提示/帮助

annotation.text_font_size = str(fig.plot_height * 0.1)+'px'

Tags: textnone图形sourcesizelinefigannotation
1条回答
网友
1楼 · 发布于 2024-03-29 10:15:08

这是一个关于css而不是bokeh本身的问题。从here开始,可以对字体大小的单位进行广泛的选择。在我的情况下,“vh”会起作用,字体大小现在对浏览器的尺寸做出响应。例如:

annotation = Label(x=10, y=10, text='text', text_font_size='10vh', text_color='white', x_units='screen', y_units='screen', background_fill_color=None)) 

独立示例:

from bokeh.server.server import Server
from bokeh.models import ColumnDataSource, Label
from bokeh.plotting import figure
from bokeh.layouts import column
import numpy as np
import datetime as dt
from functools import partial
import time

def f_emitter(p=0.1):
    v = np.random.rand()
    return (dt.datetime.now(), 0. if v>p else v)


def make_document(doc, functions, labels):
    def update():
        for index, func in enumerate(functions):
            data = func()
            sources[index].stream(new_data=dict(time=[data[0]], data=[data[1]]), rollover=1000)
            annotations[index].text = f'{data[1]: .3f}'
            # print(figs[index].height)

    sources = [ColumnDataSource(dict(time=[], data=[])) for _ in range(len(functions))]
    figs = []
    annotations = []
    for i in range(len(functions)):
        figs.append(figure(x_axis_type='datetime',
                       y_axis_label=labels[i], toolbar_location=None,
                       active_drag=None, active_scroll=None))
        figs[i].line(x='time', y='data', source=sources[i])
        annotations.append(Label(x=10, y=10, text='', text_font_size='10vh', text_color='black',
                             x_units='screen', y_units='screen', background_fill_color=None))
        figs[i].add_layout(annotations[i])


    doc.add_root(column([fig for fig in figs], sizing_mode='stretch_both'))
    doc.add_periodic_callback(callback=update, period_milliseconds=100)


if __name__ == '__main__':
    # list of functions and labels to feed into the scope
    functions = [f_emitter]
    labels = ['emitter']

    server = Server({'/': partial(make_document, functions=functions, labels=labels)})
    server.start()
    server.io_loop.add_callback(server.show, "/")
    try:
        server.io_loop.start()
    except KeyboardInterrupt:
        print('keyboard interruption')

相关问题 更多 >