调色板是RGB(A)十六进制字符串的序列(列表或元组),用于定义颜色映射,并可以设置为 color 许多打印对象的属性 bokeh.plotting . Bokeh提供许多标准的Brewer调色板,可以从 bokeh.palettes 模块。例如,导入“Spectral6”会从Brewer“Spectral”colormap中得到一个六元素的RGB(a)十六进制字符串列表。
color
bokeh.plotting
bokeh.palettes
>>> from bokeh.palettes import Spectral6 >>> Spectral6 ['#3288bd', '#99d594', '#e6f598', '#fee08b', '#fc8d59', '#d53e4f']
bokeh中包含的所有标准调色板都可以在 bokeh.palettes . 可以通过创建RGB(A)十六进制字符串序列来制作自定义调色板。
颜色映射器允许您根据序列中的值将某些数据序列编码到颜色调色板中。然后将映射器设置为 color 标记对象上的属性。Bokeh包括几种用于编码颜色的映射器:
bokeh.transform.factor_cmap :将颜色映射到特定的类别元素请参见 处理分类数据 更多细节。
bokeh.transform.factor_cmap
bokeh.transform.linear_cmap :在可用颜色中从高到低映射一系列数值。例如,一系列 [0,99] 考虑到颜色 ['red', 'green', 'blue'] 将映射如下:
bokeh.transform.linear_cmap
x < 0 : 'red' # values < low are clamped 0 >= x < 33 : 'red' 33 >= x < 66 : 'green' 66 >= x < 99 : 'blue' 99 >= x : 'blue' # values > high are clamped
bokeh.transform.log_cmap 类似于 linear_cmap 但是使用自然对数比例来绘制颜色。
bokeh.transform.log_cmap
linear_cmap
这些映射器函数返回 DataSpec 属性,该属性可以传递给glyph的color属性。包含返回的数据 bokeh.transform 可以访问它以在其他上下文中使用映射器,例如创建 ColorBar 如下面的示例所示:
DataSpec
bokeh.transform
ColorBar
from bokeh.models import ColorBar, ColumnDataSource from bokeh.palettes import Spectral6 from bokeh.plotting import figure, output_file, show from bokeh.transform import linear_cmap output_file("styling_linear_mappers.html", title="styling_linear_mappers.py example") x = [1,2,3,4,5,7,8,9,10] y = [1,2,3,4,5,7,8,9,10] #Use the field name of the column source mapper = linear_cmap(field_name='y', palette=Spectral6 ,low=min(y) ,high=max(y)) source = ColumnDataSource(dict(x=x,y=y)) p = figure(plot_width=300, plot_height=300, title="Linear Color Map Based on Y") p.circle(x='x', y='y', line_color=mapper,color=mapper, fill_alpha=1, size=12, source=source) color_bar = ColorBar(color_mapper=mapper['transform'], width=8, location=(0,0)) p.add_layout(color_bar, 'right') show(p)
为了设置Bokeh图的视觉属性的样式,您需要知道可用的属性是什么。完整的 参考文献 将单独列出每个对象的所有属性,尽管经常出现三大组属性。他们是:
线特性 线条颜色、宽度等。
填充属性 填充颜色、alpha等。
文本属性 字体样式、颜色等。
下面是每一个细节。
line_color
用于绘制线条的颜色
line_width
以像素为单位的线条宽度
line_alpha
介于0(透明)和1(不透明)之间的浮点
line_join
路径段应如何连接在一起
'miter'
'round'
'bevel'
line_cap
路径段应如何终止
'butt'
'square'
line_dash
要使用的线条样式
'solid'
'dashed'
'dotted'
'dotdash'
'dashdot'
整数像素距离的数组,用于描述要使用的虚线的开关模式
匹配正则表达式“^(\d+(\s+\d+)*)”的间隔整数字符串?$,描述了使用冲刺的开关模式
line_dash_offset
到的距离(以像素为单位) line_dash 模式应该从
fill_color
用于填充路径的颜色
fill_alpha
hatch_color
用于绘制填充图案的颜色
hatch_alpha
hatch_weight
hatch_scale
对图案“大小”的粗略测量。此值根据模式有不同的具体含义。
hatch_pattern
内置模式的字符串名称(或缩写),或中提供的模式的字符串名称 hatch_extra . 内置模式包括:
hatch_extra
FullName
缩写
例子
blank
" "
dot
"."
ring
"o"
horizontal_line
"-"
vertical_line
"|"
cross
"+"
horizontal_dash
'"'
vertical_dash
":"
spiral
"@"
right_diagonal_line
"/"
left_diagonal_line
"\\"
diagonal_cross
"x"
right_diagonal_dash
","
left_diagonal_dash
"`"
horizontal_wave
"v"
vertical_wave
">"
criss_cross
"*"
将字符串名称映射到自定义模式实现的dict。该名称可由 hatch_pattern . 例如,如果以下值设置为 hatch_extra :
hatch_extra={ 'mycustom': ImageURLTexture(url=...) }
那么名字呢 "mycustom" 可以设置为 hatch_pattern .
"mycustom"
text_font
字体名称,例如。, 'times' , 'helvetica'
'times'
'helvetica'
text_font_size
字体大小 px , em 或 pt ,例如, '16px' , '1.5em'
'16px'
'1.5em'
text_font_style
要使用的字体样式
'normal' 普通文本
'normal'
'italic' italic text
'italic'
'bold' bold text
'bold'
text_color
用于呈现文本的颜色
text_alpha
text_align
文本的水平锚定点: 'left' , 'right' , 'center'
'left'
'right'
'center'
text_baseline
文本的垂直定位点
'top'
'middle'
'bottom'
'alphabetic'
'hanging'
注解
目前只支持填充文本。一个绘制文本轮廓的界面还没有公开。
Glyph渲染器、轴、栅格和注释都具有 visible 属性,可用于打开和关闭它们。
visible
from bokeh.io import output_file, show from bokeh.plotting import figure # We set-up a standard figure with two lines p = figure(plot_width=500, plot_height=200, tools='') visible_line = p.line([1, 2, 3], [1, 2, 1], line_color="blue") invisible_line = p.line([1, 2, 3], [2, 1, 2], line_color="pink") # We hide the xaxis, the xgrid lines, and the pink line invisible_line.visible = False p.xaxis.visible = False p.xgrid.visible = False output_file("styling_visible_property.html") show(p)
这在使用Bokeh服务器或CustomJS的交互示例中特别有用。
from bokeh.io import output_file, show from bokeh.layouts import layout from bokeh.models import BoxAnnotation, Toggle from bokeh.plotting import figure output_file("styling_visible_annotation_with_interaction.html") p = figure(plot_width=600, plot_height=200, tools='') p.line([1, 2, 3], [1, 2, 1], line_color="blue") pink_line = p.line([1, 2, 3], [2, 1, 2], line_color="pink") green_box = BoxAnnotation(left=1.5, right=2.5, fill_color='green', fill_alpha=0.1) p.add_layout(green_box) # Use js_link to connect button active property to glyph visible property toggle1 = Toggle(label="Green Box", button_type="success", active=True) toggle1.js_link('active', green_box, 'visible') toggle2 = Toggle(label="Pink Line", button_type="success", active=True) toggle2.js_link('active', pink_line, 'visible') show(layout([p], [toggle1, toggle2]))
颜色属性在Bokeh中的许多地方都使用,用于指定线条、填充或文本所使用的颜色。颜色值可以通过以下任何方式提供:
任何 147 named CSS colors ,例如 'green' , 'indigo'
'green'
'indigo'
RGB(A)十六进制值,例如。, '#FF0000' , '#44444444'
'#FF0000'
'#44444444'
三元组整数 (r、g、b) 介于0和255之间
四元组 (r、g、b、a) 在哪里? r , g , b 是0到255和之间的整数 a 是介于0和1之间的浮点值
警告
提供RGB或RGBA颜色元组列表作为颜色参数(直接或作为数据源列引用)不起作用。你可以阅读我们项目中关于这个问题的讨论 GitHub 第页。建议的解决方法包括使用以下列表:
RGB十六进制值
bokeh.colors.RGB 对象(即。 [RGB(255, 0, 0), RGB(0, 255, 0)"] )
[RGB(255, 0, 0), RGB(0, 255, 0)"]
CSS格式RGB/RGBA字符串(即。 ["rgb(255, 0, 0)", "rgb(0, 255, 0)"] )
["rgb(255, 0, 0)", "rgb(0, 255, 0)"]
颜色alpha可以用多种方式指定视觉特性。这可以通过直接使用指定alpha来完成 line|fill_alpha ,或通过RGBA 4元组为 line|fill_color .
line|fill_alpha
line|fill_color
此外,还可以自由地使用两者的组合,或者根本不使用alpha。下图显示了行和填充字母输入的每个可能组合:
如果使用|博克。绘图|接口,另一个选项是指定 color and/or alpha as a keyword, as well as the demonstrated color properties. These inputs work by applying the provided value to both of the corresponding line and fill properties. However, you can still provide fill|line_alpha or fill|line_color in combination with the color/alpha 关键字,且前者优先。
alpha
line
fill
fill|line_alpha
fill|line_color
有几个 ArrowHead 可应用于箭头批注的子类型。设置 start 或 end 属性设置为“无”将导致在指定的箭头末端不应用箭头。可以通过设置这两者来创建双面箭头 start 和 end 风格。设置 visible 如果箭头为false,则相应的箭头也将不可见。
屏幕单位使用原始像素数指定高度或宽度,而数据空间单位相对于数据和绘图轴。例如,在x轴和y轴范围从0到10的400像素x 400像素图形中,宽度和高度为图形五分之一的字形将为80个屏幕单位或2个数据空间单位。
如上所述 定义关键概念 ,Bokeh plots包含表示绘图所有不同部分的对象图:网格、轴、glyphs等。要设置Bokeh图的样式,必须首先找到正确的对象,然后设置其各种属性。有些对象有方便的方法来帮助查找感兴趣的对象(请参见 Axes , Grids 和 Legends ). 但是还有一个 select() 方法对 Plot 可以更一般地用于查询Bokeh plot对象。
select()
Plot
例如,可以按类型查询对象。下面的代码段返回所有 PanTool 绘图对象有:
>>> p.select(type=PanTool) [<bokeh.models.tools.PanTool at 0x106608b90>]
这个 select() 方法也可以查询其他属性:
>>> p.circle(0, 0, name="mycircle") <bokeh.plotting.Figure at 0x106608810> >>> p.select(name="mycircle") [<bokeh.models.renderers.GlyphRenderer at 0x106a4c810>]
这种查询对于设置 Glyphs .
Plot 对象本身有许多可以设置样式的视觉特征:绘图的尺寸、背景、边框、轮廓等。本节介绍如何更改Bokeh图的这些属性。示例代码主要使用|博克。绘图|用于创建绘图的接口。但是,无论Bokeh图是如何创建的,这些说明都适用。
a的尺寸(宽度和高度) Plot 由控制 plot_width 和 plot_height 属性。这些值不是整个画布的大小,而是所有控件的大小。如果您正在使用|博克。绘图|然后将这些值传递给接口 figure() 为了方便起见:
plot_width
plot_height
figure()
from bokeh.plotting import figure, output_file, show output_file("dimensions.html") # create a new plot with specific dimensions p = figure(plot_width=700) p.plot_height = 300 p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10) show(p)
有关如何缩放打印以填充其容器的控制,请参见的文档 bokeh.models.layouts ,尤其是 sizing_mode 性质 LayoutDOM .
sizing_mode
LayoutDOM
如果你设置 sizing_mode , the plot_width 和 plot_height 当渲染绘图以填充容器时,可能会立即更改。但是,这些参数将用于计算绘图的初始纵横比,因此您可能希望保留它们。绘图只会调整到最小100px(高度或宽度),以防止显示绘图时出现问题。
打印标题的样式由的特性控制 Title 注释,可作为 .title 属性上 Plot . 大部分标准 Text Properties 除了 text_align 和 text_baseline 不适用。要相对于整个打印定位标题,请使用属性 align 和 offset .
Title
.title
align
offset
例如,要设置标题文本的颜色和字体样式,请使用 plot.title.text_color :
plot.title.text_color
from bokeh.plotting import figure, output_file, show output_file("title.html") p = figure(plot_width=400, plot_height=400, title="Some Title") p.title.text_color = "olive" p.title.text_font = "times" p.title.text_font_style = "italic" p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10) show(p)
背景填充样式由 background_fill_color 和 background_fill_alpha 的属性 Plot 对象:
background_fill_color
background_fill_alpha
from bokeh.plotting import figure, output_file, show output_file("background.html") p = figure(plot_width=400, plot_height=400) p.background_fill_color = "beige" p.background_fill_alpha = 0.5 p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10) show(p)
边框填充样式由 border_fill_color 和 border_fill_alpha 的属性 Plot 对象。您还可以使用属性设置每边的最小边框(以屏幕单位为单位)
border_fill_color
border_fill_alpha
min_border_left
min_border_right
min_border_top
min_border_bottom
另外,设置 min_border 将为方便起见,对所有边应用最小边界设置。这个 min_border 默认值为40px。
min_border
from bokeh.plotting import figure, output_file, show output_file("border.html") p = figure(plot_width=400, plot_height=400) p.border_fill_color = "whitesmoke" p.min_border_left = 80 p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) show(p)
打印区域轮廓的样式由一组 Line Properties 上 Plot 前缀为 outline_ . 例如,要设置轮廓的颜色,请使用 outline_line_color :
outline_
outline_line_color
from bokeh.plotting import figure, output_file, show output_file("outline.html") p = figure(plot_width=400, plot_height=400) p.outline_line_width = 7 p.outline_line_alpha = 0.3 p.outline_line_color = "navy" p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) show(p)
要设置字形的填充、线条或文本属性的样式,首先需要获取特定的 GlyphRenderer . 当使用|博克。绘图|接口中,glyph函数返回渲染器:
GlyphRenderer
>>> r = p.circle([1,2,3,4,5], [2,5,8,2,7]) >>> r <bokeh.models.renderers.GlyphRenderer at 0x106a4c810>
然后,字形本身从 .glyph 的属性 GlyphRenderer :
.glyph
>>> r.glyph <bokeh.models.markers.Circle at 0x10799ba10>
这是要为以下对象设置填充、线条或文本属性值的对象:
from bokeh.plotting import figure, output_file, show output_file("axes.html") p = figure(plot_width=400, plot_height=400) r = p.circle([1,2,3,4,5], [2,5,8,2,7]) glyph = r.glyph glyph.size = 60 glyph.fill_alpha = 0.2 glyph.line_color = "firebrick" glyph.line_dash = [6, 3] glyph.line_width = 2 show(p)
可以通过设置 selection_glyph 和/或 nonselection_glyph 的属性 GlyphRenderer 手动或传递给 add_glyph() .
selection_glyph
nonselection_glyph
add_glyph()
下面的图演示了如何使用|博克。绘图|接口。单击或轻触绘图上的圆以查看对选定和未选定图示符的效果。要清除选择并恢复原始状态,请单击绘图中的任意位置 外部 一个圆。
from bokeh.io import output_file, show from bokeh.models import Circle from bokeh.plotting import figure output_file("styling_selections.html") plot = figure(plot_width=400, plot_height=400, tools="tap", title="Select a circle") renderer = plot.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=50) selected_circle = Circle(fill_alpha=1, fill_color="firebrick", line_color=None) nonselected_circle = Circle(fill_alpha=0.2, fill_color="blue", line_color="firebrick") renderer.selection_glyph = selected_circle renderer.nonselection_glyph = nonselected_circle show(plot)
如果只需要设置选定或未选定glyph的颜色或alpha参数,则可以通过向glyph函数提供颜色和alpha参数(前缀为 "selection_" 或 "nonselection_" . 下图展示了这种技术:
"selection_"
"nonselection_"
from bokeh.io import output_file, show from bokeh.plotting import figure output_file("styling_selections.html") plot = figure(plot_width=400, plot_height=400, tools="tap", title="Select a circle") renderer = plot.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=50, # set visual properties for selected glyphs selection_color="firebrick", # set visual properties for non-selected glyphs nonselection_fill_alpha=0.2, nonselection_fill_color="blue", nonselection_line_color="firebrick", nonselection_line_alpha=1.0) show(plot)
模型界面也可以实现同样的效果,如下所示:
p = Plot() source = ColumnDataSource(dict(x=[1, 2, 3], y=[1, 2, 3])) initial_circle = Circle(x='x', y='y', fill_color='blue', size=50) selected_circle = Circle(fill_alpha=1, fill_color="firebrick", line_color=None) nonselected_circle = Circle(fill_alpha=0.2, fill_color="blue", line_color="firebrick") p.add_glyph(source, initial_circle, selection_glyph=selected_circle, nonselection_glyph=nonselected_circle)
Only the visual properties of selection_glyph and nonselection_glyph are considered when rendering. Changing positions, sizes, etc. will have no effect.
为悬停在上面的glyph设置突出显示策略与设置 selection_glyph 或 nonselection_glyph 或者传递前缀为 "hover_" . 下面的例子演示了后一种方法:
"hover_"
from bokeh.models import HoverTool from bokeh.plotting import figure, output_file, show from bokeh.sampledata.glucose import data output_file("styling_hover.html") subset = data.loc['2010-10-06'] x, y = subset.index.to_series(), subset['glucose'] # Basic plot setup plot = figure(plot_width=600, plot_height=300, x_axis_type="datetime", tools="", toolbar_location=None, title='Hover over points') plot.line(x, y, line_dash="4 4", line_width=1, color='gray') cr = plot.circle(x, y, size=20, fill_color="grey", hover_fill_color="firebrick", fill_alpha=0.05, hover_alpha=0.3, line_color=None, hover_line_color="white") plot.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline')) show(plot)
Only the visual properties of hover_glyph are considered when rendering. Changing positions, sizes, etc. will have no effect.
hover_glyph
一些Bokeh工具还具有可配置的可视属性。例如,各种区域选择工具和框缩放工具都具有 overlay 其线条和填充属性可以设置:
overlay
import numpy as np from bokeh.models import BoxSelectTool, BoxZoomTool, LassoSelectTool from bokeh.plotting import figure, output_file, show output_file("styling_tool_overlays.html") x = np.random.random(size=200) y = np.random.random(size=200) # Basic plot setup plot = figure(plot_width=400, plot_height=400, title='Select and Zoom', tools="box_select,box_zoom,lasso_select,reset") plot.circle(x, y, size=5) select_overlay = plot.select_one(BoxSelectTool).overlay select_overlay.fill_color = "firebrick" select_overlay.line_color = None zoom_overlay = plot.select_one(BoxZoomTool).overlay zoom_overlay.line_color = "olive" zoom_overlay.line_width = 8 zoom_overlay.line_dash = "solid" zoom_overlay.fill_color = None plot.select_one(LassoSelectTool).overlay.line_dash = [10, 10] show(plot)
除了在工具栏中选择工具外,还可以控制它们的外观。这个 autohide 属性指定只有当鼠标位于绘图区域内时工具栏才可见,否则工具栏将隐藏。
autohide
from bokeh.plotting import figure, output_file, show output_file("styling_toolbar_autohide.html") # Basic plot setup plot = figure(width=400, height=400, title='Toolbar Autohide') plot.line([1,2,3,4,5], [2,5,8,2,7]) # Set autohide to true to only show the toolbar when mouse is over plot plot.toolbar.autohide = True show(plot)
在本节中,您将学习如何更改Bokeh绘图轴的各种视觉特性。
若要设置轴对象的样式属性,请使用 xaxis , yaxis 和 axis 方法对 Plot 要首先获取绘图的轴对象,请执行以下操作:
xaxis
yaxis
axis
>>> p.xaxis [<bokeh.models.axes.LinearAxis at 0x106fa2390>]
这将返回轴对象的列表(因为可能有多个)。但请注意,为了方便起见,这些列表 喷溅的 ,这意味着您可以直接在此结果上设置属性,并且这些属性将应用于列表中的所有轴:
p.xaxis.axis_label = "Temperature"
将更改的值 axis_label 对于每个x轴(不管有多少)。
axis_label
下面的代码将设置轴的一些属性。您可以执行此代码,并尝试设置其他属性。
from bokeh.plotting import figure, output_file, show output_file("axes.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) # change just some things about the x-axis p.xaxis.axis_label = "Temp" p.xaxis.axis_line_width = 3 p.xaxis.axis_line_color = "red" # change just some things about the y-axis p.yaxis.axis_label = "Pressure" p.yaxis.major_label_text_color = "orange" p.yaxis.major_label_orientation = "vertical" # change things on all axes p.axis.minor_tick_in = -3 p.axis.minor_tick_out = 6 show(p)
轴的整个标签的文本由 axis_label 财产。此外,还有 Text Properties 前缀 axis_label_ 它控制标签的视觉外观。例如,要设置标签的颜色,请设置 axis_label_text_color . 最后,要更改轴标签和主刻度标签之间的距离,请设置 axis_label_standoff 财产:
axis_label_
axis_label_text_color
axis_label_standoff
from bokeh.plotting import figure, output_file, show output_file("bounds.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) p.xaxis.axis_label = "Lot Number" p.xaxis.axis_label_text_color = "#aa6666" p.xaxis.axis_label_standoff = 30 p.yaxis.axis_label = "Bin Count" p.yaxis.axis_label_text_font_style = "italic" show(p)
有时限制绘制轴的边界是有用的。这可以通过设置 bounds 属性转换为 (开始、结束) :
bounds
from bokeh.plotting import figure, output_file, show output_file("bounds.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) p.xaxis.bounds = (2, 4) show(p)
Bokeh有几个“ticker”模型,可以为蜱虫选择合适的位置。这些配置在 .ticker 轴的属性。和|博克。绘图|界面,选择一个合适的股票类型(分类,日期时间,墨卡托,线性或对数刻度)通常自动发生。然而,在某些情况下,更明确的控制是有用的。
.ticker
FixedTicker
这个ticker模型允许用户显式地指定准确的滴答位置,例如。
from bokeh.plotting import figure from bokeh.models.tickers import FixedTicker p = figure() # no additional tick locations will be displayed on the x-axis p.xaxis.ticker = FixedTicker(ticks=[10, 20, 37.4])
但是,也可以直接提供记号列表作为快捷方式,例如。 p.xaxis.ticker = [10, 20, 37.4] . 下面的例子演示了这种方法。
p.xaxis.ticker = [10, 20, 37.4]
from bokeh.plotting import figure, output_file, show output_file("fixed_ticks.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) p.xaxis.ticker = [2, 3.5, 4] show(p)
主要和次要记号的视觉外观由以下集合控制: Line Properties 前缀 major_tick_ and minor_tick_, respectively. For instance, to set the color of the major ticks, use major_tick_line_color. To hide either set of ticks, set the color to None. Additionally, you can control how far in and out of the plotting area the ticks extend with the properties major_tick_in/major_tick_out 和 minor_tick_in/minor_tick_out . 这些值以屏幕单位表示,负值是可以接受的。
major_tick_
minor_tick_
major_tick_line_color
None
major_tick_in
major_tick_out
minor_tick_in
minor_tick_out
from bokeh.plotting import figure, output_file, show output_file("axes.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) p.xaxis.major_tick_line_color = "firebrick" p.xaxis.major_tick_line_width = 3 p.xaxis.minor_tick_line_color = "orange" p.yaxis.minor_tick_line_color = None p.axis.major_tick_out = 10 p.axis.minor_tick_in = -3 p.axis.minor_tick_out = 8 show(p)
轴标签的文本样式由 TickFormatter 在轴上配置的对象' formatter 财产。Bokeh在不同的情况下默认使用许多ticker格式化程序:
TickFormatter
formatter
BasicTickFormatter ---线性轴的默认格式化程序。
BasicTickFormatter
CategoricalTickFormatter ---分类轴的默认格式化程序。
CategoricalTickFormatter
DatetimeTickFormatter ---日期时间轴的默认格式化程序。
DatetimeTickFormatter
LogTickFormatter ---日志轴的默认格式化程序。
LogTickFormatter
这些默认的记号格式化程序不公开许多可配置属性。要在细粒度级别控制刻度格式,请使用 NumeralTickFormatter 或 PrintfTickFormatter 如下所述。
NumeralTickFormatter
PrintfTickFormatter
若要替换轴上的记号格式化程序,必须设置 formatter 属性 Axis 对象,不在可展开列表中。这就是原因 p.yaxis[0].formatter 等等(带下标 [0] )。
Axis
p.yaxis[0].formatter
[0]
这个 NumeralTickFormatter 有一个 format 属性,可用于控制轴刻度的文本格式。
format
from bokeh.models import NumeralTickFormatter from bokeh.plotting import figure, output_file, show output_file("gridlines.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) p.xaxis[0].formatter = NumeralTickFormatter(format="0.0%") p.yaxis[0].formatter = NumeralTickFormatter(format="$0.00") show(p)
有许多其他格式可用。看到完整的 NumeralTickFormatter 中的文档 参考文献 .
这个 PrintfTickFormatter 有一个 format 属性,该属性可用于控制轴刻度的文本格式,使用 printf 样式格式字符串。
printf
from bokeh.models import PrintfTickFormatter from bokeh.plotting import figure, output_file, show output_file("gridlines.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) p.xaxis[0].formatter = PrintfTickFormatter(format="%4.1e") p.yaxis[0].formatter = PrintfTickFormatter(format="%5.3f mu") show(p)
有关格式的完整详细信息,请参见 PrintfTickFormatter 中的文档 参考文献 .
FuncTickFormatter
这个 FuncTickFormatter 允许通过提供JavaScript代码段作为 code 财产。变量 tick 将包含未格式化的tick值,并且可以预期在呈现时出现在代码段或函数命名空间中。下面的示例演示如何配置 FuncTickFormatter 来自纯JavaScript:
code
tick
from bokeh.models import FuncTickFormatter from bokeh.plotting import figure, output_file, show output_file("formatter.html") p = figure(plot_width=500, plot_height=500) p.circle([0, 2, 4, 6, 8, 10], [6, 2, 4, 10, 8, 0], size=30) p.yaxis.formatter = FuncTickFormatter(code=""" return Math.floor(tick) + " + " + (tick % 1).toFixed(2) """) show(p)
主要刻度标签的方向可以用 major_label_orientation 财产。此属性接受值 "horizontal" 或 "vertical" 或者一个浮点数,它给出从水平方向旋转的角度(以弧度为单位):
major_label_orientation
"horizontal"
"vertical"
from math import pi from bokeh.plotting import figure, output_file, show output_file("gridlines.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) p.xaxis.major_label_orientation = pi/4 p.yaxis.major_label_orientation = "vertical" show(p)
Bokeh轴支持配置更多属性。有关可以在不同类型的Bokeh轴上设置的所有不同属性的完整列表,请参阅 bokeh.models.axes 剖面图 参考文献 .
在本节中,您将学习如何在Bokeh图上设置网格线和网格带的视觉特性。
与轴的方便方法类似,有 xgrid , ygrid 和 grid 方法对 Plot 可用于获取绘图的栅格对象:
xgrid
ygrid
grid
>>> p.grid [<bokeh.models.grids.Grid at 0x106fa2278>, <bokeh.models.grids.Grid at 0x106fa22e8>]
这些方法还返回可展开列表,以便您可以将列表上的属性设置为单个对象,并且列表中的每个元素的属性都会更改:
p.grid.line_dash = [4 2]
这个 xgrid 属性提供的网格对象 横断 x轴(即垂直)。相应地, ygrid 提供与y轴相交的栅格对象(即水平)。
轴网线的视觉外观由以下集合控制: Line Properties 前缀 grid_ . 例如,要设置网格线的颜色,请使用 grid_line_color . 若要隐藏网格线,请将其线颜色设置为 None .
grid_
grid_line_color
from bokeh.plotting import figure, output_file, show output_file("gridlines.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) # change just some things about the x-grid p.xgrid.grid_line_color = None # change just some things about the y-grid p.ygrid.grid_line_alpha = 0.5 p.ygrid.grid_line_dash = [6, 4] show(p)
次要栅格线的视觉外观由以下集合控制: Line Properties 前缀 minor_grid_ . 例如,要设置网格线的颜色,请使用 minor_grid_line_color . 默认情况下,次要网格线是隐藏的(即,它们的线颜色设置为 None )
minor_grid_
minor_grid_line_color
from bokeh.plotting import figure, output_file, show output_file("minorgridlines.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) # change just some things about the y-grid p.ygrid.minor_grid_line_color = 'navy' p.ygrid.minor_grid_line_alpha = 0.1 show(p)
可以在相邻网格线之间显示填充、着色的标注栏。这些条带的视觉外观由 Fill Properties 和 Hatch Properties 前缀 band_ . 例如,要设置栅格标注栏的颜色,请使用 band_fill_color . 若要隐藏栅格标注栏,请将其填充颜色设置为 None (这是默认设置)。
band_
band_fill_color
下面是一个示例,演示用纯色填充的带:
from bokeh.plotting import figure, output_file, show output_file("grid_band_fill.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) # change just some things about the x-grid p.xgrid.grid_line_color = None # change just some things about the y-grid p.ygrid.band_fill_alpha = 0.1 p.ygrid.band_fill_color = "navy" show(p)
下面是一个示例,演示填充填充填充图案的标注栏:
from bokeh.io import output_file, show from bokeh.plotting import figure output_file("grid_band_hatch.html") p = figure(plot_height=250, plot_width=600, x_range=(0, 10), tools="", toolbar_location=None) p.line(x=[0,1,2,3,4,5,6,7,8,9,10], y=[1,3,4,3,1,2,6,5,2,3,4]) p.ygrid.grid_line_color = None ticks = [0, 2, 4, 6, 8, 10] p.xaxis[0].ticker = ticks p.xgrid[0].ticker = ticks p.xgrid.band_hatch_pattern = "/" p.xgrid.band_hatch_alpha = 0.6 p.xgrid.band_hatch_color = "lightgrey" p.xgrid.band_hatch_weight = 0.5 p.xgrid.band_hatch_scale = 10 show(p)
网格还支持设置绘制它们之间的显式边界。它们以与轴边界相同的方式设置,具有2元组 (开始、结束) :
from bokeh.plotting import figure, output_file, show output_file("bounds.html") p = figure(plot_width=400, plot_height=400) p.circle([1,2,3,4,5], [2,5,8,2,7], size=10) p.grid.bounds = (2, 4) show(p)
Bokeh网格还支持配置其他属性。有关可以在Bokeh绘图网格上设置的所有各种属性的完整列表,请参阅 bokeh.models.grids 剖面图 参考文献 .
与轴和网格的方便方法类似,有一个 legend 方法对 Plot 可以用来获得一个阴谋的 Legend 物体:
legend
Legend
>>> p.legend [<bokeh.models.annotations.Legend at 0x106fa2278>]
此方法还返回一个可展开的列表,以便您可以将列表上的属性设置为单个对象,并且列表中每个元素的属性都将更改:
p.legend.label_text_font = "times"
图例标签的位置由 location 财产。
location
对于中心布局区域中的图例,例如由 bokeh.plotting ,的值 location 可以是:
"top_left"
"top_center"
"top_right" (默认)
"top_right"
"center_right"
"bottom_right"
"bottom_center"
"bottom_left"
"center_left"
"center"
或A (x, y) 表示屏幕坐标中绝对位置的元组(从左下角开始的像素)。
(x, y)
import numpy as np from bokeh.plotting import figure, output_file, show x = np.linspace(0, 4*np.pi, 100) y = np.sin(x) output_file("legend_labels.html") p = figure() p.circle(x, y, legend_label="sin(x)") p.line(x, y, legend_label="sin(x)") p.line(x, 2*y, legend_label="2*sin(x)", line_dash=[4, 4], line_color="orange", line_width=2) p.square(x, 3*y, legend_label="3*sin(x)", fill_color=None, line_color="green") p.line(x, 3*y, legend_label="3*sin(x)", line_color="green") p.legend.location = "bottom_left" show(p)
也可以使用 add_layout 方法,但这样做需要创建 Legend 直接对象:
add_layout
import numpy as np from bokeh.models import Legend from bokeh.plotting import figure, output_file, show x = np.linspace(0, 4*np.pi, 100) y = np.sin(x) output_file("legend_labels.html") p = figure(toolbar_location="above") r0 = p.circle(x, y) r1 = p.line(x, y) r2 = p.line(x, 2*y, line_dash=[4, 4], line_color="orange", line_width=2) r3 = p.square(x, 3*y, fill_color=None, line_color="green") r4 = p.line(x, 3*y, line_color="green") legend = Legend(items=[ ("sin(x)" , [r0, r1]), ("2*sin(x)" , [r2]), ("3*sin(x)" , [r3, r4]), ], location="center") p.add_layout(legend, 'right') show(p)
在这个用例中,必须绝对指定位置。未来的版本将添加用于布局图例位置的其他选项。
图例可以有一个标题,由 title 财产:
title
plot.legend.title = "Division"
图例的标题由外观控制 Text Properties 前缀 title_ . 例如,要设置图例的字体样式,请使用 title_text_font_style .
title_
title_text_font_style
将标题与图例其余部分分开的距离(以像素为单位)由控制 title_standoff 财产。
title_standoff
import pandas as pd from bokeh.palettes import Spectral4 from bokeh.plotting import figure, output_file, show from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT output_file("styling_legend_title.html", title="styling_legend_title.py example") p = figure(plot_width=800, plot_height=250, x_axis_type="datetime") for data, name, color in zip([AAPL, IBM, MSFT, GOOG], ["AAPL", "IBM", "MSFT", "GOOG"], Spectral4): df = pd.DataFrame(data) df['date'] = pd.to_datetime(df['date']) p.line(df['date'], df['close'], line_width=2, color=color, legend_label=name) p.legend.location = "top_left" p.legend.title = 'Stock' p.legend.title_text_font_style = "bold" p.legend.title_text_font_size = "20px" show(p)
图例的方向由 orientation 财产。此属性的有效值为:
orientation
默认方向是 "vertical" .
import numpy as np from bokeh.plotting import figure, output_file, show x = np.linspace(0, 4*np.pi, 100) y = np.sin(x) output_file("legend_labels.html") p = figure() p.circle(x, y, legend_label="sin(x)") p.line(x, y, legend_label="sin(x)") p.line(x, 2*y, legend_label="2*sin(x)", line_dash=[4, 4], line_color="orange", line_width=2) p.square(x, 3*y, legend_label="3*sin(x)", fill_color=None, line_color="green") p.line(x, 3*y, legend_label="3*sin(x)", line_color="green") p.legend.orientation = "horizontal" show(p)
图例标签的视觉外观由以下集合控制: Text Properties 前缀 label_ . 例如,要设置标签的字体样式,请使用 label_text_font_style .
label_
label_text_font_style
import numpy as np from bokeh.plotting import figure, output_file, show x = np.linspace(0, 4*np.pi, 100) y = np.sin(x) output_file("legend_labels.html") p = figure() p.circle(x, y, legend_label="sin(x)") p.line(x, y, legend_label="sin(x)") p.line(x, 2*y, legend_label="2*sin(x)", line_dash=[4, 4], line_color="orange", line_width=2) p.square(x, 3*y, legend_label="3*sin(x)", fill_color=None, line_color="green") p.line(x, 3*y, legend_label="3*sin(x)", line_color="green") p.legend.label_text_font = "times" p.legend.label_text_font_style = "italic" p.legend.label_text_color = "navy" show(p)
图例边框的视觉外观由 Line Properties 前缀 border_ . 例如,要设置边框的颜色,请使用 border_line_color . 若要使边框不可见,请将边框线颜色设置为 None .
border_
border_line_color
import numpy as np from bokeh.plotting import figure, output_file, show x = np.linspace(0, 4*np.pi, 100) y = np.sin(x) output_file("legend_border.html") p = figure() p.circle(x, y, legend_label="sin(x)") p.line(x, y, legend_label="sin(x)") p.line(x, 2*y, legend_label="2*sin(x)", line_dash=[4, 4], line_color="orange", line_width=2) p.square(x, 3*y, legend_label="3*sin(x)", fill_color=None, line_color="green") p.line(x, 3*y, legend_label="3*sin(x)", line_color="green") p.legend.border_line_width = 3 p.legend.border_line_color = "navy" p.legend.border_line_alpha = 0.5 show(p)
图例背景的视觉外观由 Fill Properties 前缀 background_ . 例如,要设置背景的颜色,请使用 background_fill_color . 要使背景透明,请设置 background_fill_alpha 到 0 .
background_
0
import numpy as np from bokeh.plotting import figure, output_file, show x = np.linspace(0, 4*np.pi, 100) y = np.sin(x) output_file("legend_background.html") p = figure() p.circle(x, y, legend_label="sin(x)") p.line(x, y, legend_label="sin(x)") p.line(x, 2*y, legend_label="2*sin(x)", line_dash=[4, 4], line_color="orange", line_width=2) p.square(x, 3*y, legend_label="3*sin(x)", fill_color=None, line_color="green") p.line(x, 3*y, legend_label="3*sin(x)", line_color="green") # 3*sin(x) curve should be under this legend at initial viewing, so # we can see that the legend is transparent p.legend.location = "bottom_right" p.legend.background_fill_color = "navy" p.legend.background_fill_alpha = 0.5 show(p)
有几个属性可用于控制图例组件的布局、间距等:
label_standoff
属性类型: Int
Int
将标签与其关联的图示符分开的距离(以像素为单位)。
label_width
图例标签应占用的区域的最小宽度(以像素为单位)。
label_height
图例标签应占用的区域的最小高度(以像素为单位)。
glyph_width
渲染图例图示符应占用的宽度(以像素为单位)。
glyph_height
渲染图例图示符应占据的高度(以像素为单位)。
padding
图例内容周围的填充量。仅当边框可见时适用,否则折叠为0。
spacing
图例项之间的间距(以像素为单位)。
margin
图例周围的边距量。
import numpy as np from bokeh.plotting import figure, output_file, show x = np.linspace(0, 4*np.pi, 100) y = np.sin(x) output_file("legend_labels.html") p = figure() p.circle(x, y, legend_label="sin(x)") p.line(x, y, legend_label="sin(x)") p.line(x, 2*y, legend_label="2*sin(x)", line_dash=[4, 4], line_color="orange", line_width=2) p.square(x, 3*y, legend_label="3*sin(x)", fill_color=None, line_color="green") p.line(x, 3*y, legend_label="3*sin(x)", line_color="green") p.legend.label_standoff = 5 p.legend.glyph_width = 50 p.legend.spacing = 10 p.legend.padding = 50 p.legend.margin = 50 show(p)
Bokeh有一个渲染级别的概念来指定绘制对象的顺序:
“最低”渲染级别,在任何其他操作之前绘制
栅格的默认渲染级别
所有图示符的默认渲染级别(即高于栅格)
注释渲染器的默认渲染级别
“最高”渲染级别,用于工具覆盖
在给定级别内,渲染器按添加顺序绘制。有时显式指定渲染级别非常有用。这可以通过设置 level 参数。例如,使图像出现 在下面 网格线,您可以调用:
level
p.image(..., level="image")
您可以在本节中看到完整的输出示例 彩色映射图像 .