Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change size of labels in the Bokeh legend in python?

I have several curves (of different colours) plotted in the same figure and would like to widen the corresponding coloured lines in the legend so that they are easier to differentiate when projected onto a large screen.

I can access the properties of the legend fine, for example the legend's label text font size with:

p1.legend.label_text_font_size = "15pt"

where p1 is the figure in question. The problem is I don't know what the term for the "coloured lines" in the legend is and sadly the relevant section in the docs is empty.

like image 855
airdas Avatar asked Mar 18 '15 18:03

airdas


People also ask

How do you resize a legend?

To change the legend size of the plot, the user needs to use the cex argument of the legend function and specify its value with the user requirement, the values of cex greater than 1 will increase the legend size in the plot and the value of cex less than 1 will decrease the size of the legend in the plot.

How do I get rid of gridlines in bokeh?

You can hide the lines by setting their grid_line_color to None .


2 Answers

You can view the source code on GitHub

The only search hit for 'label_text_font_size' is in this file:-

 bokeh/bokehjs/src/coffee/renderer/annotation/legend.coffee

and scroll down to the Legend class (line 113 at the time of writing this) then you can see the class attributes. Currently they are:-

 display_defaults: ->
return _.extend {}, super(), {
level: 'overlay'
border_line_color: 'black'
border_line_width: 1
border_line_alpha: 1.0
border_line_join: 'miter'
border_line_cap: 'butt'
border_line_dash: []
border_line_dash_offset: 0
label_standoff: 15
label_text_font: "helvetica"
label_text_font_size: "10pt"
label_text_font_style: "normal"
label_text_color: "#444444"
label_text_alpha: 1.0
label_text_align: "left"
label_text_baseline: "middle"
glyph_height: 20
glyph_width: 20
label_height: 20
label_width: 50
legend_padding: 10
legend_spacing: 3
orientation: "top_right"
datapoint: null
}

.. none of them stand out as being the property that you want, so it might not be possible to change it but you might like to have a play?

NB I don't think all the properties have setters so you may have to set them using something like this: p.legend.__setattr__('label_text_color', "#FF0000")

NB Bokeh is written in CoffeeScript which I have no experience of so this may all be useless.

like image 108
jacanterbury Avatar answered Nov 15 '22 14:11

jacanterbury


The following (as proposed by @ciornav) works for me in bokeh 0.13.0:

p.legend.label_text_font_size = '20pt'

as documented here.

Be careful though where you put this code. When it's e.g. between your p.figure() and p.line() statements, it will not take effect as it seems to get overridden.

Put the code after all the artifacts have been created, before p.show() or p.save().

like image 29
hnhn Avatar answered Nov 15 '22 14:11

hnhn