如何设置Bokeh legend字体?

时间:2014-07-09 07:09:33

标签: python bokeh

编辑:请注意下面的示例代码的语法已经过时了,从早期版本的Bokeh开始。


如何定义用于标记所有内容的字体?我只想知道如何更改标题,标记和轴的字体,但如何更改图例的字体?

以下是测试更改的运行示例。

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

4 个答案:

答案 0 :(得分:6)

我尝试了提议:

p.legend().label_text_font = "times"

但是我收到了一个错误:

TypeError: '_list_attr_splat' object is not callable

然而,当我遗漏括号时,它工作正常:

p.legend.label_text_font = "times"

如果label_text_font不是您想要的,您通常可以获得图例的可用属性列表,或者另一个列表:list_attr_splat&#39;对象,通过引入超级有用的错误消息:

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

答案 1 :(得分:5)

您需要获取当前绘图的Legend个对象,可以使用legend绘图属性完成,然后设置label_text_font属性:

plot.legend.label_text_font = "times"

请注意,这些属性名称可能会在不久的将来缩短/简化。

答案 2 :(得分:4)

在搜索时到达此页面的人的额外信息&#34;如何将图例移动到另一个角落?&#34;:

legend().orientation = "top_left"

接受条款top_lefttop_rightbottom_leftbottom_right

答案 3 :(得分:0)

尝试一下:

p.legend.orientation = "horizontal"