Bokeh 简明教程
Bokeh - Axes
在本章中,我们讨论几种不同类型的轴。
In this chapter, we shall discuss about various types of axes.
Sr.No |
Axes |
Description |
1 |
Categorical Axes |
The bokeh plots show numerical data along both x and y axes. In order to use categorical data along either of axes, we need to specify a FactorRange to specify categorical dimensions for one of them. |
2 |
Log Scale Axes |
If there exists a power law relationship between x and y data series, it is desirable to use log scales on both axes. |
3 |
Twin Axes |
It may be needed to show multiple axes representing varying ranges on a single plot figure. The figure object can be so configured by defining extra_x_range and extra_y_range properties |
Categorical Axes
在迄今为止的示例中,Bokeh 图表沿着 x 和 y 轴显示数值数据。为了沿任一轴使用分类数据,我们需要指定 FactorRange 来为其中一个分类维度。例如,要在 x 轴上使用给定列表中的字符串 −
In the examples so far, the Bokeh plots show numerical data along both x and y axes. In order to use categorical data along either of axes, we need to specify a FactorRange to specify categorical dimensions for one of them. For example, to use strings in the given list for x axis −
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
fig = figure(x_range = langs, plot_width = 300, plot_height = 300)
Example
通过以下示例,显示一个简单的条形图,显示注册了各种课程的学生数量。
With following example, a simple bar plot is displayed showing number of students enrolled for various courses offered.
from bokeh.plotting import figure, output_file, show
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
fig = figure(x_range = langs, plot_width = 300, plot_height = 300)
fig.vbar(x = langs, top = students, width = 0.5)
show(fig)
Output

要以不同颜色显示每个条形,可将 vbar() 函数的 color 属性设置为一个颜色值列表。
To show each bar in different colour, set color property of vbar() function to list of color values.
cols = ['red','green','orange','navy', 'cyan']
fig.vbar(x = langs, top = students, color = cols,width=0.5)
Output

要使用 vbar_stack() 或 hbar_stack() 函数呈现垂直(或水平)堆叠条形,将 stackers 属性设置要依次堆叠的字段列表,source 属性设置为一个包含与每个字段相应的 dict 对象。
To render a vertical (or horizontal) stacked bar using vbar_stack() or hbar_stack() function, set stackers property to list of fields to stack successively and source property to a dict object containing values corresponding to each field.
在以下示例中,sales 是一个字典,显示了三个产品在三个月内的销售额。
In following example, sales is a dictionary showing sales figures of three products in three months.
from bokeh.plotting import figure, output_file, show
products = ['computer','mobile','printer']
months = ['Jan','Feb','Mar']
sales = {'products':products,
'Jan':[10,40,5],
'Feb':[8,45,10],
'Mar':[25,60,22]}
cols = ['red','green','blue']#,'navy', 'cyan']
fig = figure(x_range = products, plot_width = 300, plot_height = 300)
fig.vbar_stack(months, x = 'products', source = sales, color = cols,width = 0.5)
show(fig)
Output

通过使用 bokeh.transform 模块中的 dodge() 函数,对条形指定可视位移,便可获得分组条形图。
A grouped bar plot is obtained by specifying a visual displacement for the bars with the help of dodge() function in bokeh.transform module.
dodge() function 为每个条形图增加一个相对偏移量,从而在视觉上营造出分组效果。在以下示例中, vbar() glyph 针对某个月的特定条形组以 0.25 的偏移量分开。
The dodge() function introduces a relative offset for each bar plot thereby achieving a visual impression of group. In following example, vbar() glyph is separated by an offset of 0.25 for each group of bars for a particular month.
from bokeh.plotting import figure, output_file, show
from bokeh.transform import dodge
products = ['computer','mobile','printer']
months = ['Jan','Feb','Mar']
sales = {'products':products,
'Jan':[10,40,5],
'Feb':[8,45,10],
'Mar':[25,60,22]}
fig = figure(x_range = products, plot_width = 300, plot_height = 300)
fig.vbar(x = dodge('products', -0.25, range = fig.x_range), top = 'Jan',
width = 0.2,source = sales, color = "red")
fig.vbar(x = dodge('products', 0.0, range = fig.x_range), top = 'Feb',
width = 0.2, source = sales,color = "green")
fig.vbar(x = dodge('products', 0.25, range = fig.x_range), top = 'Mar',
width = 0.2,source = sales,color = "blue")
show(fig)
Log Scale Axes
当图表中某一轴上的值随另一轴上值线性增加而呈指数增长时,通常需要在对数刻度上显示前轴上的数据。例如,如果 x 和 y 数据序列之间存在幂律关系,则最好在两个轴上使用对数刻度。
When values on one of the axes of a plot grow exponentially with linearly increasing values of another, it is often necessary to have the data on former axis be displayed on a log scale. For example, if there exists a power law relationship between x and y data series, it is desirable to use log scales on both axes.
Bokeh.plotting API 的 figure() 函数接受 x_axis_type 和 y_axis_type 作为参数,可通过为这些参数中的任何一个传递“log”值来将其指定为对数轴。
Bokeh.plotting API’s figure() function accepts x_axis_type and y_axis_type as arguments which may be specified as log axis by passing "log" for the value of either of these parameters.
第一幅图以线性刻度绘制 x 和 10x 之间的图。在第二幅图中,y_axis_type 设置为“log”。
First figure shows plot between x and 10x on a linear scale. In second figure y_axis_type is set to 'log'
from bokeh.plotting import figure, output_file, show
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y = [10**i for i in x]
fig = figure(title = 'Linear scale example',plot_width = 400, plot_height = 400)
fig.line(x, y, line_width = 2)
show(fig)
Twin Axes
在某些情况下,在单个图表上显示多个表示不同范围的轴可能必不可少。figure 对象可通过定义 extra_x_range 和 extra_y_range 属性进行配置。在向 figure 添加新的 glyph 时,将使用这些命名的范围。
In certain situations, it may be needed to show multiple axes representing varying ranges on a single plot figure. The figure object can be so configured by defining extra_x_range and extra_y_range properties. While adding new glyph to the figure, these named ranges are used.
我们尝试在同一图上显示正弦曲线和直线。两种 glyph 的 y 轴的范围不同。正弦曲线和直线的 x 和 y 数据序列通过以下内容获取 −
We try to display a sine curve and a straight line in same plot. Both glyphs have y axes with different ranges. The x and y data series for sine curve and line are obtained by the following −
from numpy import pi, arange, sin, linspace
x = arange(-2*pi, 2*pi, 0.1)
y = sin(x)
y2 = linspace(0, 100, len(y))
在此,x和y之间的图解表示正弦关系,x和y2之间的图解表示直线。使用显式y范围定义Figure对象,并添加表示正弦曲线的线形字形,如下所示:
Here, plot between x and y represents sine relation and plot between x and y2 is a straight line. The Figure object is defined with explicit y_range and a line glyph representing sine curve is added as follows −
fig = figure(title = 'Twin Axis Example', y_range = (-1.1, 1.1))
fig.line(x, y, color = "red")
我们需要一个额外的y范围。定义为:
We need an extra y range. It is defined as −
fig.extra_y_ranges = {"y2": Range1d(start = 0, end = 100)}
要在右侧添加额外的y轴,请使用add_layout()方法。在图像中添加表示x和y2的新线形字形。
To add additional y axis on right side, use add_layout() method. Add a new line glyph representing x and y2 to the figure.
fig.add_layout(LinearAxis(y_range_name = "y2"), 'right')
fig.line(x, y2, color = "blue", y_range_name = "y2")
这将生成一个具有两个y轴的图。完整代码和输出如下:
This will result in a plot with twin y axes. Complete code and the output is as follows −
from numpy import pi, arange, sin, linspace
x = arange(-2*pi, 2*pi, 0.1)
y = sin(x)
y2 = linspace(0, 100, len(y))
from bokeh.plotting import output_file, figure, show
from bokeh.models import LinearAxis, Range1d
fig = figure(title='Twin Axis Example', y_range = (-1.1, 1.1))
fig.line(x, y, color = "red")
fig.extra_y_ranges = {"y2": Range1d(start = 0, end = 100)}
fig.add_layout(LinearAxis(y_range_name = "y2"), 'right')
fig.line(x, y2, color = "blue", y_range_name = "y2")
show(fig)