我是给一个图添加一个标签作为注释。我可以在前期设置标签的字体大小。但是,当调整浏览器的大小时,只有图的大小是有反应的,标签的字体大小是没有反应的。
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))
我试着用图的高度来调整字体大小,但这是行不通的。有什么方法可以达到这个目的吗?谢谢你的任何提示help。
annotation.text_font_size = str(fig.plot_height * 0.1)+'px'
这更多的是css的问题,而不是散景本身的问题。来自 此处对于字体大小的单位,我们有广泛的选择范围。对于我的情况,'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')