如何在 Python 中使用 Bokeh 在圖表上視覺化多種形狀?
Bokeh 是一個用於資料視覺化的 Python 包。它是一個開源專案。Bokeh 使用 HTML 和 JavaScript 渲染其圖表。這表明它在使用基於 Web 的儀表板時非常有用。
Bokeh 將資料來源轉換為 JSON 檔案。此檔案用作 BokehJS 的輸入,BokehJS 是一個 JavaScript 庫。這個 BokehJS是用TypeScript編寫的,有助於在現代瀏覽器上渲染視覺化效果。
Matplotlib 和 Seaborn 生成靜態圖表,而 Bokeh 生成互動式圖表。這意味著當用戶與這些圖表互動時,它們會相應地改變。
圖表可以嵌入為 Flask 或 Django 啟用 Web 應用程式的輸出。Jupyter notebook 也可用於渲染這些圖表。
Bokeh 的依賴項:
Numpy Pillow Jinja2 Packaging Pyyaml Six Tornado Python−dateutil
在 Windows 命令提示符下安裝 Bokeh
pip3 install bokeh
在 Anaconda 提示符下安裝 Bokeh
conda install bokeh
以下是一個示例:
From bokeh.plotting import figure, output_file, show
示例
my_fig = figure(plot_width = 400, plot_height = 300) my_fig.rect(x = 11,y = 11,width = 150, height = 75, width_units = 'screen', height_units = 'screen') my_fig.square(x = 2,y = 3,size = 80, color = 'blue') my_fig.ellipse(x = 7,y = 6, width = 30, height = 10, fill_color = None, line_width = 2) my_fig.oval(x = 6,y = 6,width = 2, height = 1, angle = -0.4) show(my_fig)
輸出
解釋
匯入併為所需的包設定別名。
呼叫 figure 函式以及圖表寬度和高度。
呼叫 'output_file' 函式來指定將生成的 html 檔案的名稱。
呼叫 Bokeh 中的 'rect'、'square'、'ellipse' 和 'oval' 函式以及資料。
使用 'show' 函式顯示圖表。
廣告