更改 Bokeh 标签注释的文字大小
Change the text size of Bokeh label annotations
我正在为 Bokeh 条形图添加标签注释:
labels = LabelSet(x='roomsavailable', y='area', text='roomsavailable', level='glyph',
x_offset=-15, y_offset=-13.5, source=source, render_mode='canvas')
p.add_layout(labels)
有谁知道是否可以调整文本的大小?
In the docs 它描述了 text_font_size
属性:
The text font size values for the text.
所以试试
YOUR_FONT_SIZE = 10
labels = LabelSet(x='stock',
y='area',
text='roomsavailable',
text_font_size=YOUR_FONT_SIZE,
level='glyph',
x_offset=-15,
y_offset=-13.5,
source=source,
render_mode='canvas')
text_font_size 接受一个字符串值,例如
YOUR_FONT_SIZE = '10pt'
您还可以添加以下代码:
p.xaxis.axis_label_text_font_size = '15pt'
p.yaxis.axis_label_text_font_size = '15pt'
这是对@MarkWeston 已经存在的答案的扩展。
让我们看看下面这个最小的例子:
from bokeh.io import show
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.plotting import figure
p = figure()
source = ColumnDataSource(dict(x=[x for x in range(1,8)],
y=[1]*7,
names=[str(x) for x in range(1,8)]
)
)
p = figure(x_range=(0, 8), y_range=(0, 2), plot_height=100, tools='' )
labels = LabelSet(x='x', y='y', text='names', level='overlay', text_align='center',
x_offset=0, y_offset=-8, source=source, render_mode='canvas')
p.add_layout(labels)
p.circle(x='x', y='y', radius=0.3, alpha=0.3, source=source)
p.xgrid.visible = False
p.ygrid.visible = False
p.xaxis.visible = False
p.yaxis.visible = False
show(p)
这是 example 的输出。
现在我们可以按照@MarkWeston 的回答,预先设置我们想要的参数。
如果创建 LabelSet
,则可以传递 documentation of LabelSet.
中解释的所有参数
但是还有第二种方法,如果你想在调用p.add_layout()
后更改你的设置。
函数 p.add_layout(labels)
带有一个名为 place
的参数。有效值为 left
、right
、above
、below
和默认值 center
。有关更多信息,另请参阅 documentation to add_layout.
为简化起见,我们假设您使用默认值 center
调用 add_layout()
函数。现在我们需要知道 p.center
我们的 LabelSet
中包含的列表的索引位置。
我们可以通过运行
得到这些信息
[i for i, item in enumerate(p.center) if isinstance(item, LabelSet)]
>>> [2]
这将 return “2”。请注意,在 0 和 1 处有不可见的网格。
现在我们可以更改此 LabelSet
调用行的参数设置,例如
p.center[2].text_color = {'value':'#0000ff'}
p.center[2].text_size = {'value':'11pt'}
p.center[2].text_size = 'TimesNewRoman'
还有更多。
如果你用 show(p)
再次绘制图形,你可以看到变化,它看起来像
this.
我正在为 Bokeh 条形图添加标签注释:
labels = LabelSet(x='roomsavailable', y='area', text='roomsavailable', level='glyph',
x_offset=-15, y_offset=-13.5, source=source, render_mode='canvas')
p.add_layout(labels)
有谁知道是否可以调整文本的大小?
In the docs 它描述了 text_font_size
属性:
The text font size values for the text.
所以试试
YOUR_FONT_SIZE = 10
labels = LabelSet(x='stock',
y='area',
text='roomsavailable',
text_font_size=YOUR_FONT_SIZE,
level='glyph',
x_offset=-15,
y_offset=-13.5,
source=source,
render_mode='canvas')
text_font_size 接受一个字符串值,例如 YOUR_FONT_SIZE = '10pt'
您还可以添加以下代码:
p.xaxis.axis_label_text_font_size = '15pt'
p.yaxis.axis_label_text_font_size = '15pt'
这是对@MarkWeston 已经存在的答案的扩展。
让我们看看下面这个最小的例子:
from bokeh.io import show
from bokeh.models import ColumnDataSource, LabelSet
from bokeh.plotting import figure
p = figure()
source = ColumnDataSource(dict(x=[x for x in range(1,8)],
y=[1]*7,
names=[str(x) for x in range(1,8)]
)
)
p = figure(x_range=(0, 8), y_range=(0, 2), plot_height=100, tools='' )
labels = LabelSet(x='x', y='y', text='names', level='overlay', text_align='center',
x_offset=0, y_offset=-8, source=source, render_mode='canvas')
p.add_layout(labels)
p.circle(x='x', y='y', radius=0.3, alpha=0.3, source=source)
p.xgrid.visible = False
p.ygrid.visible = False
p.xaxis.visible = False
p.yaxis.visible = False
show(p)
这是 example 的输出。
现在我们可以按照@MarkWeston 的回答,预先设置我们想要的参数。
如果创建 LabelSet
,则可以传递 documentation of LabelSet.
但是还有第二种方法,如果你想在调用p.add_layout()
后更改你的设置。
函数 p.add_layout(labels)
带有一个名为 place
的参数。有效值为 left
、right
、above
、below
和默认值 center
。有关更多信息,另请参阅 documentation to add_layout.
为简化起见,我们假设您使用默认值 center
调用 add_layout()
函数。现在我们需要知道 p.center
我们的 LabelSet
中包含的列表的索引位置。
我们可以通过运行
得到这些信息[i for i, item in enumerate(p.center) if isinstance(item, LabelSet)]
>>> [2]
这将 return “2”。请注意,在 0 和 1 处有不可见的网格。
现在我们可以更改此 LabelSet
调用行的参数设置,例如
p.center[2].text_color = {'value':'#0000ff'}
p.center[2].text_size = {'value':'11pt'}
p.center[2].text_size = 'TimesNewRoman'
还有更多。
如果你用 show(p)
再次绘制图形,你可以看到变化,它看起来像
this.