如何在 Bokeh 中設定座標軸限制?
Bokeh 是一個內建的 Python 庫,用於建立高度互動式的資料視覺化圖形。通常,Web 開發人員使用此庫來構建功能強大的儀表板、即時應用程式以及簡單的 Web 圖表。在 Python 中,我們有一些 Bokeh 內建函式,例如 figure()、vbar()、y_range() 等,用於設定 Bokeh 的座標軸限制。各種領域都使用此庫,例如資料科學家、資料工程師和 Web 開發人員。
語法
以下語法在示例中使用:
figure()
figure 是 Bokeh 庫的內建方法,它是 plot 的一個子類,用於建立繪圖。
vbar()
vbar() 是一個內建方法,它遵循 Bokeh 庫,並透過渲染具有給定中心座標的垂直條形來顯示結果。
grid_line_color()
這是 Bokeh 庫中網格的公共資料屬性,用於設定網格線的顏色。
y_range()
y_range() 遵循來自 bokeh.models 內容的 Range1d 模組,允許我們設定座標軸的起點和終點。
line()
line() 方法使用一些參考引數(如 line_width 和 color)來設定圖形的線條。
安裝要求:
pip install bokeh
此命令安裝 Bokeh 庫所需的所有依賴項。
示例 1
在以下示例中,首先從 bokeh.plotting 內容中匯入模組 **figure、show**。然後我們有兩個軸 - factors(表示不同字元的列表)和 x(表示 x 軸)。接下來,使用內建函式 figure(),它接受名為 y_range 的引數,其值為 factors 以設定 y 軸的範圍,並將其儲存在變數 plot_ax 中。接下來使用內建方法 circle 作為物件引用,變數為 plot_ax,它為座標軸設定點圓圈。最後,它使用 show 函式,該函式接受名為 plot_ax 的引數以獲取結果。
from bokeh.plotting import figure, show factors = ["a", "b", "c", "d"] x = [25, 37, 80, 60] plot_ax = figure(y_range=factors) plot_ax.circle(x, factors, size=15, fill_color="orange", line_color="green", line_width=3) show(plot_ax)
輸出
示例 2
在以下示例中,首先,從 bokeh.models 內容中匯入名為 Range1d 的模組。然後在變數 fig 中使用函式 figure,透過使用兩個引數 **plot_width** 和 **plot_height** 來設定圖形的繪圖長度。接下來,在變數 fig 中設定 x_range 和 y_range,並透過分配 Range1d 來設定值,這將設定 x 和 y 軸上的座標軸限制。接下來使用 line 和 circle 作為物件新增到變數 fig 中以繪製圖形。最後,我們藉助內建方法 show() 列印輸出,該方法接受引數 fig。
from bokeh.plotting import figure, show from bokeh.models import Range1d fig = figure(plot_width=500, plot_height=500) fig.x_range = Range1d(20, 25) fig.y_range = Range1d(35, 100) fig.line([12, 22, 33, 14, 2], [6, 7, 5, 9, 5], line_width=2, color="red") fig.circle([1, 2, 3, 4, 5], [56, 82, 10, 20, 26], size=30, color="green", alpha=0.8) show(fig)
輸出
示例 3
在以下示例中,我們將展示如何設定 x 和 y 軸上的限制。對於 x 軸,它將使用內建函式 x_range,而對於 y 軸,它將使用內建函式 y_range。然後它使用另一個稱為 circle 的內建函式,該函式設定為座標軸上的點。最後,使用 show() 函式獲取結果。
from bokeh.plotting import figure, show from bokeh.models import Range1d fig = figure(width=500, height=400, x_range=(0, 30)) fig.y_range = Range1d(0, 15) fig.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=10) show(fig)
輸出
示例 4
在以下示例中,首先匯入必要的模組。然後設定兩個變數,即 products - 用於表示商品列表和 price - 設定單個商品的價格。接下來,使用內建方法 figure,它接受一些引數:
x_range - 將所有變數名稱 products 列表設定為圖形上 x 軸的限制。
height - 將其值設定為整數以設定圖形的高度。
title - 設定主標題。
toolbar_location - 透過將值設定為 none 來固定圖形的常數位置。
tools - 設定空雙引號,這將固定圖形的常數位置。
所有這些 figure 引數都儲存在變數 p 中。然後使用 vbar() 方法設定垂直條形,該方法接受一些引數 - x(將值設定為 product 以顯示圖形圖 x 軸上的所有商品)、top(將值設定為變數 price 以設定 y 軸上的限制)和 width(設定 Bokeh 圖形寬度)。現在使用以下方法設定網格線顏色:
# import the module from bokeh.plotting import figure, show products = ['PEN', 'NOTEBOOK', 'ERASER', 'SHARPNER', 'GEOMETRY BOX', 'CHARTPAPER'] # set the axis limit price = [5, 30, 10, 12, 80, 15] p = figure(x_range=products, height=350, title="PRODUCT AND PRICE GRAPH", toolbar_location=None, tools="") p.vbar(x=products, top=price, width=0.9) p.xgrid.grid_line_color = None p.y_range.start = 0 show(p)
輸出
結論
我們討論了使用 Bokeh 模組設定 x 軸和 y 軸限制的各種方法。Bokeh 庫執行高級別的資料視覺化,使圖形具有很強的互動性。Bokeh 的替代庫有 Plotly.js、Matplotlib、Dash、D3.js 和 Tableau。