小目錄子
組合圖表
設定軸範圍
指定軸型別
1.分類軸
2.時間軸
3.對數軸
4.雙軸軸
組合圖表
呼叫多個圖表函式可以製作組合圖表:
from bokeh。plotting import figure, output_file, show
x = [1, 2, 3, 4, 5]
y = [6, 7, 8, 7, 3]
output_file(“multiple。html”)
p = figure(plot_width=400, plot_height=400)
# 呼叫折線圖和散點圖
p。line(x, y, line_width=2)
p。circle(x, y, fill_color=“white”, size=8)
show(p)
這個示例可能跟大家之前理解的組合圖表不太一樣,畢竟這個圖在Excel中折線圖加標記就可以搞定。所以,框架結構不一樣的地方還需要大家多適應。
另外,這種方式可以在同一個圖形中新增不限數量的圖表。
設定範圍
預設情況下,Bokeh會自動設定繪圖的資料邊界以適合資料,但有時你可能需要設定一個繪圖的範圍,這可以透過使用Range1d物件設定x_range或y_range的屬性來完成,這個物件提供了所需範圍的開始點和結束點:
p。x_range = Range1d(0,100)
為了方便起見,figure()函式還可以接受(start,end)元組作為x_range或y_range引數的值。 下面是一個示例,顯示了設定範圍的兩種方法:
from bokeh。plotting import figure, output_file, show
from bokeh。models import Range1d
output_file(“title。html”)
# 建立一個圖表,設定大小和橫軸範圍
p = figure(plot_width=400, plot_height=400, x_range=(0, 20))
# 使用Range1d物件設定縱軸範圍
p。y_range = Range1d(0, 15)
p。circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10)
show(p)
範圍還可以接受最小和最大屬性,這樣就可以指定不希望使用者能夠平移/縮放的圖的邊緣。
指定軸型別
上面的示例都是使用的預設線性軸,適用於需要以線性刻度顯示數字資料的繪圖。 而我們可能還會有分類資料,或需要在日期時間或對數座標軸上顯示資料。這部分將介紹如何在使用bokeh。plotting介面時指定軸型別。
分類軸
分類軸是透過為其中一個軸指定因子範圍來建立的。
from bokeh。plotting import figure, output_file, show
factors = [“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”]
x = [50, 40, 65, 10, 25, 37, 80, 60]
output_file(“categorical。html”)
p = figure(y_range=factors)
p。circle(x, factors, size=15, fill_color=“orange”, line_color=“green”, line_width=3)
show(p)
時間軸
在處理時間序列資料或涉及日期或時間的資料時,有一個可顯示適合不同日期和時間標度的標籤的軸是極好的。
我們已經看到如何使用figure。p()函式使用bokeh。plotting介面建立圖。 該函式接受x_axis_type和y_axis_type作為引數。 要指定日期時間軸,請將“datetime”作為這些引數中的任何一個的值。
import pandas as pd
from bokeh。plotting import figure, output_file, show
from bokeh。sampledata。stocks import AAPL
df = pd。DataFrame(AAPL)
df[‘date’] = pd。to_datetime(df[‘date’])
output_file(“datetime。html”)
# 建立一個帶有時間軸的圖
p = figure(plot_width=800, plot_height=250, x_axis_type=“datetime”)
p。line(df[‘date’], df[‘close’], color=‘navy’, alpha=0。5)
show(p)
對數軸
當處理指數增長或者數量級資料的資料時,通常需要使用一個對數刻度軸。 另一種情況是涉及繪製具有冪律關係的資料,當需要在兩個軸上使用對數刻度時。
正如我們上面看到的,figure()函式接受x_axis_type和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**xx for xx in x]
output_file(“log。html”)
# 建立一個帶有對數軸的圖
p = figure(plot_width=400, plot_height=400, y_axis_type=“log”)
p。line(x, y, line_width=2)
p。circle(x, y, fill_color=“white”, size=8)
show(p)
雙軸
此外,我們還可以將多個代表不同範圍的軸新增到一個繪圖中,使用extra_x_range和extra_y_range命名額外的軸範圍來配置圖表。 然後,在新增新的圖形時可以引用這些命名的範圍,還可以在繪圖上使用add_layout方法新增新的軸物件。 下面給出一個例子:
from numpy import pi, arange, sin, linspace
from bokeh。plotting import output_file, figure, show
from bokeh。models import LinearAxis, Range1d
x = arange(-2*pi, 2*pi, 0。1)
y = sin(x)
y2 = linspace(0, 100, len(y))
output_file(“twin_axis。html”)
p = figure(x_range=(-6。5, 6。5), y_range=(-1。1, 1。1))
p。circle(x, y, color=“red”)
p。extra_y_ranges = {“foo”: Range1d(start=0, end=100)}
p。circle(x, y2, color=“blue”, y_range_name=“foo”)
p。add_layout(LinearAxis(y_range_name=“foo”), ‘left’)
show(p)
如果將倒數第二行的“left”修改為“right”,則左右側各有一軸。