样式化绘图元素#
选择绘图对象#
如果您想自定义 Bokeh 绘图的任何元素的外观,首先需要确定要修改的对象。正如简介中所述,Bokeh 绘图是 Python 对象的组合,这些对象代表绘图的所有不同部分:例如,网格、坐标轴和字形。
某些对象具有方便的方法来帮助您识别要寻址的对象。请参阅样式化坐标轴、样式化网格和样式化图例以获取示例。
要查询任何 Bokeh 绘图对象,请在Plot上使用select()方法。例如,要查找绘图中的所有PanTool对象
>>> p.select(type=PanTool)
[<bokeh.models.tools.PanTool at 0x106608b90>]
您还可以使用select()方法查询其他属性
>>> p.circle(0, 0, radius=1, name="mycircle")
<bokeh.plotting.figure at 0x106608810>
>>> p.select(name="mycircle")
[<bokeh.models.renderers.GlyphRenderer at 0x106a4c810>]
当您想要样式化样式化字形的视觉属性时,此查询方法尤其有用。
样式化绘图#
除了单个绘图元素之外,Plot对象本身也具有多个您可以自定义的视觉特征:例如,绘图的尺寸、背景、边框或轮廓。本节介绍如何更改 Bokeh 绘图的这些属性。
示例代码主要使用bokeh.plotting接口来创建绘图。但是,这些说明适用于 Bokeh 绘图的任何创建方式。
尺寸#
要更改Plot的宽度和高度,请使用其width和height属性。这两个属性使用屏幕单位。它们控制整个画布区域的大小,包括任何坐标轴或标题(但不包括工具栏)。
如果您正在使用bokeh.plotting接口,则可以将这些值直接传递给figure()
from bokeh.plotting import figure, show
# create a new plot with specific dimensions
p = figure(width=700)
p.height = 300
p.scatter([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
show(p)
响应式尺寸#
要自动调整绘图的宽度或高度以适应浏览器中的可用空间,请使用绘图的sizing_mode属性。
要控制绘图如何缩放以填充其容器,请参阅布局的文档,特别是LayoutDOM的sizing_mode属性。
如果您将sizing_mode设置为除fixed之外的任何值,Bokeh 会在绘图渲染后立即调整width和height。但是,Bokeh 使用width和height来计算绘图的初始纵横比。
为了防止绘图显示出现问题,绘图只会向下调整大小到最小 100 像素(高度或宽度)。
标题#
要样式化绘图的标题,请使用Title注解,它作为Plot的.title属性提供。
您可以使用大多数标准的文本属性。但是,text_align和text_baseline不适用。要相对于整个绘图定位标题,请改用属性align和offset。
例如,要设置标题文本的颜色和字体样式,请使用plot.title.text_color
from bokeh.plotting import figure, show
p = figure(width=400, height=400, title="Some Title")
p.title.text_color = "olive"
p.title.text_font = "times"
p.title.text_font_style = "italic"
p.scatter([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
show(p)
背景#
要更改背景填充样式,请调整Plot对象的background_fill_color和background_fill_alpha属性
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.background_fill_color = "beige"
p.background_fill_alpha = 0.5
p.scatter([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
show(p)
边框#
要调整边框填充样式,请使用Plot对象的border_fill_color和border_fill_alpha属性。您还可以使用以下属性设置每侧的最小边框(以屏幕单位为单位)
min_border_leftmin_border_rightmin_border_topmin_border_bottom
此外,如果您设置min_border,Bokeh 会为方便起见将最小边框设置应用于所有边。 min_border默认值为 40px。
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.border_fill_color = "whitesmoke"
p.min_border_left = 80
p.scatter([1,2,3,4,5], [2,5,8,2,7], size=10)
show(p)
轮廓#
Bokeh Plot对象具有各种线条属性。要更改轮廓的外观,请使用以outline_为前缀的那些线条属性。
例如,要设置轮廓的颜色,请使用outline_line_color
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.outline_line_width = 7
p.outline_line_alpha = 0.3
p.outline_line_color = "navy"
p.scatter([1,2,3,4,5], [2,5,8,2,7], size=10)
show(p)
样式化字形#
要样式化字形的填充、线条或文本属性,您首先需要确定要自定义的GlyphRenderer。如果您正在使用bokeh.plotting接口,则字形函数会返回渲染器
>>> r = p.circle([1,2,3,4,5], [2,5,8,2,7], radius=1)
>>> r
<bokeh.models.renderers.GlyphRenderer at 0x106a4c810>
接下来,从GlyphRenderer的.glyph属性中获取字形本身
>>> r.glyph
<bokeh.models.glyphs.Circle at 0x10799ba10>
这是设置填充、线条或文本属性值的对象
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
r = p.scatter([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)
选定和未选定字形#
要自定义选定和未选定字形的样式,请设置GlyphRenderer的selection_glyph和nonselection_glyph属性。您可以手动设置它们,也可以将它们传递给add_glyph()。
下面的绘图使用bokeh.plotting接口来设置这些属性。单击或点击绘图上的任何圆圈以查看对选定和未选定字形的影响。要清除选择并恢复到原始状态,请单击圆圈外部绘图中的任何位置。
from bokeh.plotting import figure, show
plot = figure(width=400, height=400, tools="tap", title="Select a circle")
renderer = plot.scatter([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=50)
renderer.selection_glyph = renderer.glyph.clone(fill_alpha=1, fill_color="firebrick", line_color=None)
renderer.nonselection_glyph = renderer.glyph.clone(fill_alpha=0.2, fill_color="blue", line_color="firebrick")
show(plot)
如果您只需要设置选定或未选定字形的颜色或 alpha 参数,请为字形函数提供颜色和 alpha 参数,并以"selection_"或"nonselection_"为前缀
from bokeh.plotting import figure, show
plot = figure(width=400, height=400, tools="tap", title="Select a circle")
renderer = plot.scatter(
x=[1, 2, 3, 4, 5],
y=[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)
如果您使用bokeh.models接口,请使用add_glyph()函数
p = Plot()
source = ColumnDataSource(dict(x=[1, 2, 3], y=[1, 2, 3]))
initial_circle = Circle(x='x', y='y', fill_color='blue', radius=1)
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)
悬停检查#
要样式化悬停在上面的字形的外观,请将以"hover_"为前缀的颜色或 alpha 参数传递给渲染器函数。
或者,设置GlyphRenderer的selection_glyph和nonselection_glyph属性,就像上面的选定和未选定字形中一样。
此示例使用第一种方法,即传递带有"hover_"前缀的颜色参数
from bokeh.models import RELATIVE_DATETIME_CONTEXT, HoverTool
from bokeh.plotting import figure, show
from bokeh.sampledata.glucose import data
x = data.loc['2010-10-06'].index.to_series()
y = data.loc['2010-10-06']['glucose']
# Basic plot setup
p = figure(width=800, height=400, x_axis_type="datetime",
tools="pan,wheel_zoom", title='Hover over points')
p.xaxis.formatter.context = RELATIVE_DATETIME_CONTEXT()
p.ygrid.grid_line_color = None
p.background_fill_color = "#fafafa"
p.line(x, y, line_dash="4 4", line_width=1, color='gray')
cr = p.scatter(
x, y, size=20,
fill_color="steelblue", alpha=0.1, line_color=None,
hover_fill_color="midnightblue", hover_alpha=0.5,
hover_line_color="white",
)
p.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline'))
show(p)
覆盖非视觉属性#
字形允许覆盖任何数据驱动的属性,而不仅仅是视觉属性。这样,用户可以例如在悬停时更改散点图中标记的大小,甚至将字形从其原始位置偏移。实际上,用户可以用完全不相关的字形覆盖主字形(GlyphRenderer.glyph)(例如,将Circle替换为Rect)。
注意
只有主字形用于命中测试和其他功能。辅助字形(selection_glyph、hover_glyph等)用于绘制,并且仅影响字形的外观。
此示例显示了如何覆盖Circle字形的非视觉属性(悬停时的radius)以及如何在选择时使用不同的字形
import numpy as np
from bokeh.core.properties import field
from bokeh.io import show
from bokeh.models import BoxAnnotation, Indexed, Rect, Tooltip
from bokeh.palettes import Spectral11
from bokeh.plotting import figure
N = 50
x = np.random.random(size=N)*100
y = np.random.random(size=N)*100
radii = np.random.uniform(1, 5, size=N)
radii_big = radii*2
colors = np.random.choice(Spectral11, size=N)
p = figure(tools=["hover", "box_select"], active_drag="box_select")
cr = p.circle(
x, y, radius=radii,
fill_color=colors, fill_alpha=0.8, line_color=None,
hover_fill_alpha=0.5, # mix `hover_` attributes with manual setup below
)
# there is no `hover_radius` so we have set things manually
cr.data_source.data["radii_big"] = radii_big
cr.hover_glyph.radius = field("radii_big")
# make selection glyph unrelated while reusing existing data
cr.selection_glyph = Rect(
line_color=None,
fill_color=field("fill_color"),
width=field("radii_big"),
height=field("radius"),
)
p.hover.tooltips = None
tooltip = Tooltip(position=Indexed(renderer=cr, index=0), content="Hover over me!", visible=True)
p.elements.append(tooltip)
box = BoxAnnotation(left=40, right=80, top=80, bottom=40)
tooltip = Tooltip(position=box.nodes.top_center, content="Select me!", visible=True, attachment="above")
box.elements.append(tooltip)
p.renderers.append(box)
show(p)
样式化坐标轴#
本节重点介绍更改 Bokeh 绘图坐标轴的各种视觉属性。
要在 Axis 对象上设置样式属性,请使用xaxis、yaxis和axis方法在Plot上首先获取绘图的 Axis 对象。例如
>>> p.xaxis
[<bokeh.models.axes.LinearAxis at 0x106fa2390>]
因为可能存在多个坐标轴,所以此方法返回 Axis 对象列表。但是,为了方便起见,这些列表是可展开的。这意味着您可以直接在此结果上设置属性,并且这些属性将应用于列表中的所有坐标轴。例如
p.xaxis.axis_label = "Temperature"
这将更改p的每个 x 轴的axis_label的值,无论有多少个。
下面的示例更详细地演示了xaxis、yaxis和axis方法的用法
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([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属性。要在坐标轴标签的文本中添加换行符,请在字符串中包含\n。
要控制标签文本的视觉外观,请使用任何以axis_label_为前缀的标准文本属性。例如,要设置标签的文本颜色,请设置axis_label_text_color。
要更改坐标轴标签与主刻度标签之间的距离,请设置axis_label_standoff属性。
例如
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([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属性设置为(开始,结束)的 2 元组
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([1,2,3,4,5], [2,5,8,2,7], size=10)
p.xaxis.bounds = (2, 4)
show(p)
刻度位置#
Bokeh 使用多个“ticker”模型来决定在坐标轴上显示刻度的位置(分类、日期时间、墨卡托、线性或对数刻度)。要配置刻度的位置,请使用坐标轴的.ticker属性。
如果您使用bokeh.plotting接口,Bokeh 会自动选择合适的刻度位置模型。
如果您需要控制要使用的刻度位置模型,您还可以显式定义刻度位置列表。将带有刻度位置列表的FixedTicker分配给坐标轴
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])
作为快捷方式,您还可以直接将刻度列表提供给坐标轴的ticker属性
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([1,2,3,4,5], [2,5,8,2,7], size=10)
p.xaxis.ticker = [2, 3.5, 4]
show(p)
CustomJSTicker#
要完全自定义坐标轴刻度的位置,请结合 JavaScript 代码段使用CustomJSTicker作为其major_code和minor_code属性。
这些代码段应返回刻度位置列表
from bokeh.models import CustomJSTicker
from bokeh.plotting import figure, show
xticker = CustomJSTicker(
# always three equally spaced ticks
major_code="""
const {start, end} = cb_data.range
const interval = (end-start) / 4
return [start + interval, start + 2*interval, start + 3*interval]
""",
# minor ticks in between the major ticks
minor_code="""
const {start, end, major_ticks} = cb_data
return [
(start+major_ticks[0])/2,
(major_ticks[0]+major_ticks[1])/2,
(major_ticks[1]+major_ticks[2])/2,
(major_ticks[2]+end)/2,
]
""",
)
yticker = CustomJSTicker(major_code="return ['a', 'c', 'e', 'g']")
p = figure(y_range=list("abcdefg"))
p.scatter([1, 2, 3, 4, 5], ["a", "d", "b", "f", "c"], size=30)
p.xaxis.ticker = xticker
# keep the grid lines at all original tick locations
p.ygrid.ticker = p.yaxis.ticker
p.yaxis.ticker = yticker
show(p)
刻度线#
要控制主刻度和次刻度的视觉外观,请设置适当的线条属性,分别以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, show
p = figure(width=400, height=400)
p.scatter([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)
刻度标签格式#
要样式化坐标轴标签的文本,请使用坐标轴formatter属性的TickFormatter对象。 Bokeh 默认在不同情况下使用多个刻度格式化程序
BasicTickFormatter— 线性轴的默认格式化程序。CategoricalTickFormatter— 分类轴的默认格式化程序。DatetimeTickFormatter— 日期时间轴的默认格式化程序。LogTickFormatter— 对数轴的默认格式化程序。
这些默认的刻度格式化程序没有公开很多可配置的属性。要在更精细的级别控制刻度格式,请使用下面描述的NumeralTickFormatter或PrintfTickFormatter之一。
注意
要替换坐标轴上的刻度格式化程序,您必须在实际的Axis对象上设置formatter属性,而不是在可展开的列表上。这就是为什么以下示例使用p.yaxis[0].formatter等(带下标[0])。
NumeralTickFormatter#
NumeralTickFormatter具有format属性,可用于控制坐标轴刻度的文本格式。
from bokeh.models import NumeralTickFormatter
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([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#
PrintfTickFormatter具有format属性,可用于使用printf样式格式字符串控制坐标轴刻度的文本格式。
from bokeh.models import PrintfTickFormatter
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([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文档中的完整参考指南。
CustomJSTickFormatter#
要完全自定义坐标轴刻度的格式,请结合 JavaScript 代码段使用CustomJSTickFormatter作为其code属性。
变量tick包含未格式化的刻度值。在渲染时,它可以在代码段或函数命名空间中访问
from bokeh.models import CustomJSTickFormatter
from bokeh.plotting import figure, show
p = figure(width=500, height=500)
p.scatter([0, 2, 4, 6, 8, 10], [6, 2, 4, 10, 8, 0], size=30)
p.yaxis.formatter = CustomJSTickFormatter(code="""
return Math.floor(tick) + " + " + (tick % 1).toFixed(2)
""")
show(p)
日期时间刻度上下文#
日期时间刻度格式化程序具有其他属性,用于为日期时间轴上的刻度添加更多上下文。例如,上下文格式可能会在第一个刻度上显示年、月和日,而常规刻度显示小时和分钟。
这在坐标轴可缩放的情况下尤其有用。例如:当放大到秒级时,刻度格式化程序上下文可以提供有关更广泛的时间单位(例如天或月)的其他信息。
上下文选项是
context用于为
context_which指定的刻度或刻度添加上下文的格式。值是None,不添加上下文
标准
DatetimeTickFormatter格式字符串,此单一格式用于所有比例另一个
DatetimeTickFormatter实例,用于添加与比例相关的上下文
context_which要将格式化上下文字符串添加到哪个刻度或哪些刻度。值是:
"start"、"end"、"center"和"all"。context_location相对于刻度标签文本基线,应在何处渲染上下文。值是:
"below"、"above"、"left"和"right"。
有一个预定义的RELATIVE_DATETIME_CONTEXT,它添加的上下文或多或少比单个比例高。下面的示例演示了这些选项
from bokeh.models import RELATIVE_DATETIME_CONTEXT
from bokeh.plotting import figure, show
from bokeh.sampledata.glucose import data
x = data.loc['2010-10-06'].index.to_series()
y = data.loc['2010-10-06']['glucose']
p = figure(sizing_mode="stretch_width", x_axis_type="datetime",
tools="xwheel_zoom")
p.xaxis.formatter.context = RELATIVE_DATETIME_CONTEXT()
p.line(x, y, line_dash="4 4", line_width=3, color='gray')
show(p)
可以将多个DatetimeTickFormatter实例“链接”在一起,以获得所需数量的上下文级别。例如
p.xaxis.formatter.context = DatetimeTickFormatter(...)
p.xaxis.formatter.context.context = DatetimeTickFormatter(...)
刻度标签方向#
要控制主刻度标签的方向,请使用major_label_orientation属性。此属性接受值"horizontal"或"vertical"或浮点数,该浮点数给出从水平方向旋转的角度(以弧度为单位)
from math import pi
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([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 绘图上网格线和网格带的视觉属性。
要获取绘图的 Grid 对象,请使用xgrid、ygrid和grid方法在Plot上。这与坐标轴的便捷方法类似
>>> p.grid
[<bokeh.models.grids.Grid at 0x106fa2278>,
<bokeh.models.grids.Grid at 0x106fa22e8>]
这些方法也返回可展开的列表。您可以像在单个对象上一样在列表上设置属性,并且该属性将为列表的每个元素更改
p.grid.line_dash = [4 2]
注意
xgrid属性提供与 x 轴相交的网格对象(表示垂直方向的对象)。相应地,ygrid提供与 y 轴相交的网格对象(表示水平方向的对象)。
线条#
要配置网格线的视觉外观,请使用以grid_为前缀的线条属性集合。
例如,要设置网格线的颜色,请使用grid_line_color。要隐藏网格线,请将其线条颜色设置为None
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([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)
次要线条#
要配置次要网格线的视觉外观,请使用以minor_grid_为前缀的线条属性集合。
例如,要设置网格线的颜色,请使用minor_grid_line_color。默认情况下,次要网格线是隐藏的(这意味着它们的线条颜色设置为None)
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([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)
带#
使用“带”来显示相邻网格线之间填充的阴影带。要控制这些带的视觉外观,请使用以band_为前缀的填充属性和阴影线属性集合。
例如,要设置网格带的颜色,请使用band_fill_color。要隐藏网格带,请将其填充颜色设置为None(这是默认值)。
这个例子定义了用纯色填充的带状区域
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([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.plotting import figure, show
p = figure(height=250, 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)
边界#
要设置明确的边界来限制网格的绘制位置,请使用 (开始, 结束) 的二元组。这与设置 坐标轴的边界 相同
from bokeh.plotting import figure, show
p = figure(width=400, height=400)
p.scatter([1,2,3,4,5], [2,5,8,2,7], size=10)
p.grid.bounds = (2, 4)
show(p)
注意
Bokeh 网格还支持配置其他属性。有关可以在 Bokeh 图表网格上设置的各种属性的完整列表,请查阅 参考指南 的网格部分。
图例样式#
与坐标轴和网格的便捷方法类似,Plot 上有一个 legend() 方法,您可以使用它来获取图表的 Legend 对象
bokeh.models.plots.Plot.legend
>>> p.legend
[<bokeh.models.annotations.Legend at 0x106fa2278>]
此方法还返回一个可展开的列表。因此,您可以像设置单个对象一样设置列表的属性,并且该属性将为列表的每个元素更改
p.legend.label_text_font = "times"
位置#
要控制图例标签的位置,请使用 location 属性。
图表区域内部#
对于中心布局区域中的图例,例如由 bokeh.plotting 自动创建的图例,请将 location 设置为以下值之一
"top_left"
"top_center"
"top_right" (默认)
"center_right"
"bottom_right"
"bottom_center"
"bottom_left"
"center_left"
"center"
或指示屏幕坐标中绝对位置的 (x, y) 元组(从左下角开始的像素)。
import numpy as np
from bokeh.plotting import figure, show
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
p = figure()
p.scatter(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.scatter(x, 3*y, marker="square", 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 对象
import numpy as np
from bokeh.models import Legend
from bokeh.plotting import figure, show
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
p = figure(toolbar_location="above")
r0 = p.scatter(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.scatter(x, 3*y, marker="square", 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 属性
plot.legend.title = "Division"
要控制图例标题的视觉外观,请使用任何以 title_ 为前缀的标准 文本属性。例如,要设置图例的字体样式,请使用 title_text_font_style。
要设置标题与图例其余部分之间的距离(以像素为单位),请使用 title_standoff 属性。
import pandas as pd
from bokeh.palettes import Spectral4
from bokeh.plotting import figure, show
from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT
p = figure(width=800, 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 属性。此属性的有效值为
"vertical""horizontal"
默认方向为 "vertical"。
import numpy as np
from bokeh.plotting import figure, show
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
p = figure()
p.scatter(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.scatter(x, 3*y, marker="square", 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)
二维布局#
通过将正整数设置为属性 nrows 或 ncols,可以为图例激活二维布局。这提供了避免图例被截断的可能性。
nrows 和 ncols 的默认值为 "auto",如果 orientation 属性为 "vertical",则导致一列;如果 orientation 属性为 "horizontal",则导致一行。
import numpy as np
from bokeh.layouts import column
from bokeh.plotting import figure, show
x = np.linspace(0, 4*np.pi, 100)
sinx = np.sin(x)
p1 = figure(title='Default legend layout', width=500, height=300)
[p1.line(x, (1 + i/20)*sinx, legend_label=f"{1+i/20:.2f}*sin(x)") for i in range(7)]
p2 = figure(title='Legend layout with 2 columns', width=500, height=300)
[p2.line(x, (1 + i/20)*sinx, legend_label=f"{1+i/20:.2f}*sin(x)") for i in range(7)]
p2.legend.ncols=2
p3 = figure(title='Legend layout with 3 rows', width=500, height=300)
[p3.line(x, (1 + i/20)*sinx, legend_label=f"{1+i/20:.2f}*sin(x)") for i in range(7)]
p3.legend.nrows=3
show(column(p1, p2, p3))
标签文本#
要控制图例标签的视觉外观,请使用任何以 label_ 为前缀的标准 文本属性。例如,要设置标签的字体样式,请使用 label_text_font_style。
import numpy as np
from bokeh.plotting import figure, show
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
p = figure()
p.scatter(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.scatter(x, 3*y, marker="square", 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)
边框#
要控制图例边框的视觉外观,请使用一组以 border_ 为前缀的 线条属性。例如,要设置边框的颜色,请使用 border_line_color。要使边框不可见,请将边框线条颜色设置为 None。
import numpy as np
from bokeh.plotting import figure, show
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
p = figure()
p.scatter(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.scatter(x, 3*y, marker="square", 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)
背景#
要控制图例背景的视觉外观,请使用一组以 background_ 为前缀的 填充属性。例如,要设置背景颜色,请使用 background_fill_color。要使背景透明,请将 background_fill_alpha 设置为 0。
import numpy as np
from bokeh.plotting import figure, show
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
p = figure()
p.scatter(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.scatter(x, 3*y, marker="square", 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)
尺寸#
要控制尺寸,例如标签组件的布局或间距,请使用以下属性
有几个属性可用于控制图例组件的布局、间距等
- padding = 10#
- 类型:
Either(NonNegative,Tuple(NonNegative,NonNegative),Struct,Tuple(NonNegative,NonNegative,NonNegative,NonNegative),Struct)
图例内容与其边框之间的内边距量。
仅在边框可见时适用。
import numpy as np
from bokeh.plotting import figure, show
x = np.linspace(0, 4*np.pi, 100)
y = np.sin(x)
p = figure()
p.scatter(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.scatter(x, 3*y, marker="square", 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)
设置渲染级别#
要指定事物的绘制顺序,请使用以下渲染级别之一
- image:
“最低”渲染级别,在任何其他内容之前绘制
- underlay:
网格的默认渲染级别
- glyph:
所有字形的默认渲染级别(这意味着它们在网格之上绘制)
- annotation:
注释渲染器的默认渲染级别
- overlay:
“最高”渲染级别,用于工具覆盖
在给定的级别内,渲染器按照添加顺序绘制。
要显式指定渲染级别,请在渲染器上使用 level 参数。
例如,要确保图像在网格线下方渲染,请在调用 image 渲染器时,将渲染级别 "image" 分配给 level 参数
p.image(..., level="image")
您可以在 颜色映射图像 部分中看到包含输出的完整示例。