Change the text size of Bokeh label annotations

Viewed 5935

I'm adding label annotations to Bokeh bar chart :

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)

Does anyone know if it's possible to adjust the size of the text?

4 Answers

text_font_size takes in a string value like YOUR_FONT_SIZE = '10pt'

In the docs it describes the text_font_size attribute:

The text font size values for the text.

So try something like

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

You can also add the following code:

p.xaxis.axis_label_text_font_size = '15pt'
p.yaxis.axis_label_text_font_size = '15pt'

This is an extantion to the already existing answer by @MarkWeston.

Let's take a look to this minimal example below:

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)

This is the output of the example.

Now we can follow the answer by @MarkWeston and set the parameter we want beforehand. If you create a LabelSet you can pass all parameters explained in the documentation of LabelSet.

But there is also a second way, if you want to change your settings after calling p.add_layout().

The function p.add_layout(labels) comes with a parameter called place. Valid values are left, right, above, below and the default value center. For more see also the documentation to add_layout.

For simplification let's assume that you call the add_layout() function with the default value center. Now we need to now, at which index of the list contained in p.center our LabelSet is placed.

We can get this information by running

[i for i, item in enumerate(p.center) if isinstance(item, LabelSet)]
>>> [2]

which will return "2". Note that at 0 and 1 there are the invisible grids.

Now we can change our parameter settings of this LabelSet calling lines like

p.center[2].text_color = {'value':'#0000ff'}
p.center[2].text_size = {'value':'11pt'}
p.center[2].text_size = 'TimesNewRoman'

and many more.

If you plot the figure again with show(p) you can see the changes and it looks like this.

Related