Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Bokeh legend font?

Tags:

python

bokeh

EDIT: Please note the syntax of the example code below is out of date, from a very early version of Bokeh.


How to define the font used for labeling everything? I only figured out how to change the font of the title, the markes and the axis, but how do I change the font of the legend?

Here is a running example for testing the changes.

from bokeh.plotting import *
from bokeh.sampledata import periodic_table
import pandas as pd

elements = periodic_table.elements
elements = elements[elements['atomic number'] <= 82]
elements = elements[~pd.isnull(elements['melting point'])]
mass = [float(x.strip('[]')) for x in elements['atomic mass']]
elements['atomic mass'] = mass

palette = list(reversed([
    '#67001f','#b2182b','#d6604d','#f4a582','#fddbc7','#f7f7f7','#d1e5f0','#92c5de','#4393c3','#2166ac','#053061'
]))

melting_points = elements['melting point']
low = min(melting_points)
high= max(melting_points)
melting_point_inds = [int(10*(x-low)/(high-low)) for x in melting_points] #gives items in colors a value from 0-10
meltingpointcolors = [palette[i] for i in melting_point_inds]

output_file("elements.html", title="elements.py example")

hold()

circle(elements['atomic mass'], elements['density'] ,
       color=meltingpointcolors, plot_width=1200, line_color='black',fill_alpha=0.8,
       size=12, title='Density vs Atomic Weight of Elements (colored by melting point)', legend="circle",
       title_text_font="times", background_fill= '#cccccc', tools='pan, wheel_zoom, box_zoom, reset')

text(elements['atomic mass'], elements['density'] +0.3,
    text=elements['symbol'],angle=0, text_color='#333333',
    text_align="center", text_font_size="10pt", text_font="times")

xaxis().axis_label='atomic weight (amu)'
yaxis().axis_label='density (g/cm^3)'
grid().grid_line_color='white'
axis().axis_label_text_font="times"
show()
like image 714
user2366975 Avatar asked Jul 09 '14 07:07

user2366975


People also ask

How do I save HTML in bokeh?

Bokeh creates the HTML file when you call the show() function. This function also automatically opens a web browser to display the HTML file. If you want Bokeh to only generate the file but not open it in a web browser, use the save() function instead.

How do I get rid of gridlines in bokeh?

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


3 Answers

I tried the proposed:

p.legend().label_text_font = "times"

but I got an error:

TypeError: '_list_attr_splat' object is not callable

however it worked fine when I left out the parentheses:

p.legend.label_text_font = "times"

If label_text_font is not what you want, you can often get a list of the available attributes for legend, or another 'list_attr_splat' object, by inducing the super helpful error message:

p.legend.blah = "foo"

AttributeError: unexpected attribute 'blah' to Legend, 
possible attributes are border_line_alpha, border_line_cap,
border_line_color, border_line_dash, border_line_dash_offset, 
border_line_join, border_line_width, glyph_height, glyph_width,
label_height, label_standoff, label_text_align, label_text_alpha,
label_text_baseline, label_text_color, label_text_font,
label_text_font_size, label_text_font_style, label_width, 
legend_padding, legend_spacing, legends, name, orientation, 
plot, session or tags
like image 24
sharon Avatar answered Oct 08 '22 04:10

sharon


You need to get ahold of the Legend object(s) of the current plot which can be done with legend plot attribute and then set the label_text_font property:

plot.legend.label_text_font = "times"

note these property names may be shortened/simplified in the near future.

like image 144
bigreddot Avatar answered Oct 08 '22 06:10

bigreddot


extra information for those who reach this page while searching on "how to move the legend to another corner?":

legend().orientation = "top_left"

acceptables terms top_left, top_right, bottom_left and bottom_right.

like image 21
ThomasL Avatar answered Oct 08 '22 06:10

ThomasL