- Bokeh 教程
- Bokeh - 首頁
- Bokeh - 簡介
- Bokeh - 環境設定
- Bokeh - 入門
- Bokeh - Jupyter Notebook
- Bokeh - 基本概念
- Bokeh - 使用 Glyph 繪製圖形
- Bokeh - 面積圖
- Bokeh - 圓形 Glyph
- Bokeh - 矩形、橢圓和多邊形
- Bokeh - 扇形和弧形
- Bokeh - 專用曲線
- Bokeh - 設定範圍
- Bokeh - 座標軸
- Bokeh - 註釋和圖例
- Bokeh - Pandas
- Bokeh - ColumnDataSource
- Bokeh - 過濾資料
- Bokeh - 佈局
- Bokeh - 繪圖工具
- Bokeh - 樣式化視覺屬性
- Bokeh - 自定義圖例
- Bokeh - 新增小部件
- Bokeh - 伺服器
- Bokeh - 使用 Bokeh 子命令
- Bokeh - 匯出圖形
- Bokeh - 嵌入圖形和應用程式
- Bokeh - 擴充套件 Bokeh
- Bokeh - WebGL
- Bokeh - 使用 JavaScript 開發
- Bokeh 有用資源
- Bokeh - 快速指南
- Bokeh - 有用資源
- Bokeh - 討論
Bokeh - 座標軸
在本章中,我們將討論各種型別的座標軸。
| 序號 | 座標軸 | 描述 |
|---|---|---|
| 1 | 分類座標軸 | Bokeh 圖表在 x 軸和 y 軸上顯示數值資料。為了在任一軸上使用分類資料,我們需要指定一個 FactorRange 來為其中之一指定分類維度。 |
| 2 | 對數刻度座標軸 | 如果 x 和 y 資料序列之間存在冪律關係,則最好在兩個軸上都使用對數刻度。 |
| 3 | 雙座標軸 | 可能需要在一個圖形上顯示多個表示不同範圍的座標軸。可以透過定義 **extra_x_range** 和 **extra_y_range** 屬性來配置圖形物件。 |
分類座標軸
在之前的示例中,Bokeh 圖表在 x 軸和 y 軸上都顯示了數值資料。為了在任一軸上使用分類資料,我們需要指定一個 FactorRange 來為其中之一指定分類維度。例如,要使用給定列表中的字串作為 x 軸:
langs = ['C', 'C++', 'Java', 'Python', 'PHP'] fig = figure(x_range = langs, plot_width = 300, plot_height = 300)
示例
透過以下示例,顯示了一個簡單的條形圖,顯示了不同課程的註冊學生人數。
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)
輸出
要以不同的顏色顯示每個條形,請將 vbar() 函式的 color 屬性設定為顏色值的列表。
cols = ['red','green','orange','navy', 'cyan'] fig.vbar(x = langs, top = students, color = cols,width=0.5)
輸出
要使用 vbar_stack() 或 hbar_stack() 函式渲染垂直(或水平)堆疊條形圖,請將 stackers 屬性設定為要依次堆疊的欄位列表,並將 source 屬性設定為包含每個欄位對應值的字典物件。
在以下示例中,sales 是一個字典,顯示了三個產品在三個月的銷售額。
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)
輸出
透過使用 **bokeh.transform** 模組中的 dodge() 函式為條形指定視覺偏移,可以獲得分組條形圖。
**dodge() 函式** 為每個條形圖引入了相對偏移,從而實現了組的視覺效果。在以下示例中,**vbar() glyph** 為特定月份的每組條形圖偏移了 0.25。
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)
輸出
對數刻度座標軸
當圖形的一個軸上的值隨著另一個軸的線性增長而呈指數增長時,通常需要以對數刻度顯示前一個軸上的資料。例如,如果 x 和 y 資料序列之間存在冪律關係,則最好在兩個軸上都使用對數刻度。
Bokeh.plotting API 的 figure() 函式接受 x_axis_type 和 y_axis_type 作為引數,可以透過為這兩個引數中的任何一個傳遞 "log" 來將其指定為對數軸。
第一個圖形顯示了 x 和 10x 之間線上性刻度上的圖形。在第二個圖形中,y_axis_type 設定為 '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)
輸出
現在更改 figure() 函式以配置 y_axis_type='log'
fig = figure(title = 'Linear scale example',plot_width = 400, plot_height = 400, y_axis_type = "log")
輸出
雙座標軸
在某些情況下,可能需要在一個圖形上顯示多個表示不同範圍的座標軸。可以透過定義 **extra_x_range** 和 **extra_y_range** 屬性來配置圖形物件。在向圖形新增新 glyph 時,將使用這些命名範圍。
我們嘗試在同一圖形中顯示正弦曲線和直線。兩個 glyph 的 y 軸具有不同的範圍。正弦曲線和直線的 x 和 y 資料序列如下所示:
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_range,並添加了表示正弦曲線的線 glyph,如下所示:
fig = figure(title = 'Twin Axis Example', y_range = (-1.1, 1.1)) fig.line(x, y, color = "red")
我們需要一個額外的 y 範圍。它定義如下:
fig.extra_y_ranges = {"y2": Range1d(start = 0, end = 100)}
要在右側新增額外的 y 軸,請使用 add_layout() 方法。向圖形新增一個表示 x 和 y2 的新線 glyph。
fig.add_layout(LinearAxis(y_range_name = "y2"), 'right') fig.line(x, y2, color = "blue", y_range_name = "y2")
這將生成一個帶有雙 y 軸的圖形。完整的程式碼和輸出如下:
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)
輸出