# Bokeh 探索頻道（1）：Python 互動式圖表函數庫初體驗

台股研究 2020年7月20日 約 2 分鐘閱讀

Python Bokeh 互動式圖表函數庫初體驗：不寫 JavaScript 也能做出 D3.js 等級的動態股價視覺化，搭配 AAPL/IBM/MSFT 範例與 HoverTool、ColumnDataSource、Click policy 設定，將 matplotlib 圖表升級成可互動 HTML。

## Python 視覺化套件使用經驗

Python套件多，品質不一。用module之前，我習慣先看github星星確認認可度，有1000個以上多是品質保證，再看release history，看是否有被持續維護，那種2年以上沒新版本的，採坑機率高。Bokeh看來沒問題。

[![Bokeh GitHub 星星數與維護狀態確認](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/y3p5KBo-1024x246.png)](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/y3p5KBo-1024x246.png)

依本文重跑

## 執行這個策略

複製提示詞，讓 AI 依照上方證據的資料、條件與風險檢查重跑一次。

告訴你的AI：

幫我設定 FinLab，重現這篇文章，請讀：https\://finlab.finance/setup?relatedUrl=/blog/python-bokeh-interactive-charts-intro

[免安裝，開啟 FinLab Studio](https://studio.finlab.finance/chat?lang=zh\&research_context=%7B%22v%22%3A1%2C%22source_url%22%3A%22https%3A%2F%2Ffinlab.finance%2Fblog%2Fpython-bokeh-interactive-charts-intro%22%7D)

平常python視覺化的主力套件是matplotlib和seaborn，前者是20歲老套件，是視覺化元祖，許多套件基礎都是建構在matplotlib上，像seaborn就是，有更簡潔的寫法和精美圖例，兩者都以靜態視覺居多，缺乏變化性（或是要費點功）。 Python守備範圍廣，但在網頁前端仍難以與Java Script抗衡，而D3.js函數庫也要花不少學習成本，有沒有辦法用Python做出JS動態視覺的效果？

Bokeh的出世，提供了Python與D3.js之間的橋梁，讓你可以用Python做出D3.js常用功能的效果，減少學習成本，高效開發，輕鬆描述統計結果。

## 厲害在哪裡？

先來看Gallory，python視覺化套件很多，範例圖不夠精美的，可考慮跳過。

1. 化學元素週期表

[![Bokeh 繪製的化學元素週期表範例](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/81XiMAU-1-1024x459.png)](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/81XiMAU-1-1024x459.png)

2. 雷達圖

[![Bokeh 繪製的雷達圖範例](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/hMPxBgq-1-1024x1000.png)](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/hMPxBgq-1-1024x1000.png)

3. 互動式wiget散點圖表

[![Bokeh 互動式 widget 散點圖表範例](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/9vnv8yZ-1-1024x613.png)](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/9vnv8yZ-1-1024x613.png)

逛了一圈，發現Bokeh圖表精美、範例多、都有附原始碼，官方教學也完整，有tutorial jupyter檔案教學。 看來比matplotlib強大，也比plotly漂亮…，函數庫使用看來也不難，且可鑲嵌到Django等Web框架，無論是local或遠端的前端呈現都可以效率開發。

## 開箱試玩時間

要注意的是bokeh會預設連BokehJS cdn，但連線有時不是很穩定，這時可多加”INLINE”環境變數設定，讓BokehJS驅動於local python env。

顯示程式碼

```
from bokeh.resources import INLINE
bokeh.io.output_notebook(INLINE)
```

Bokeh可以在Jupyter呈現開發也可以跳轉出html檔，可自由設定，預設是跳轉html檔（output\_file()）。若要更改預設，必須加上`bokeh.io.reset_output()`重設環境預設。

顯示程式碼

```
output_notebook() ＃jupyter呈現
output_file()  ＃html呈現
```

fig物件可設定tools參數，圖表會自帶縮放、重整、儲存等功能。 以下是參考官方範例後微調的程式。

顯示程式碼

```
from bokeh.plotting import figure, output_file, show, output_notebook
import bokeh.io
from bokeh.resources import INLINE
 
# env settings 環境變數設定
bokeh.io.reset_output()
bokeh.io.output_notebook(INLINE)
 
# prepare some data
x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y0 = [i**2 for i in x]
y1 = [10**i for i in x]
y2 = [10**(i**2) for i in x]
 
# output to static HTML file
# output_file("log_lines.html")
 
# create a new plot
p = figure(
tools="pan,box_zoom,reset,save",
y_axis_type="log", y_range=[0.001, 10**11], title="log axis example",
x_axis_label='sections', y_axis_label='particles'
)
 
# add some renderers
p.line(x, x, legend_label="y=x")
p.circle(x, x, legend_label="y=x", fill_color="white", size=8)
p.line(x, y0, legend_label="y=x^2", line_width=3)
p.line(x, y1, legend_label="y=10^x", line_color="red")
p.circle(x, y1, legend_label="y=10^x", fill_color="red", line_color="red", size=6)
p.line(x, y2, legend_label="y=10^x^2", line_color="orange", line_dash="4 4")
 
# show the results
show(p)
output_notebook() 
```

[![Bokeh 繪製的對數座標折線圖範例結果](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/cK9ZFBB-1024x717.png)](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/cK9ZFBB-1024x717.png)

## 投資圖表試玩

Bokeh官方有提供sample\_data給大家練習，gallery豐富的範例都取自sample\_data，對比官方的資料格式就能輕鬆模仿應用，沒啥高深程式技巧。下載sample\_data指令為`bokeh.sampledata.download()`，直接貼在jupyter執行。檔案會下載到bokeh module裡。

## 檢查蘋果電腦範例資料（json）

顯示程式碼

```
from bokeh.sampledata.stocks import AAPL
# 檢查欄位
AAPL.keys()
#dict_keys(['date', 'open', 'high', 'low', 'close', 'volume', 'adj_close'])
```

## ColumnDataSource物件為Bokeh資料驅動渲染核心

dataframe要傳入ColumnDataSource才能驅動js。 providing the data that is visualized by the glyphs of the plot <https://docs.bokeh.org/en/latest/docs/user_guide/data.html>

## HoverTool

游標滑過時顯示資料

## Click\_policy

藉由標籤控制數值顯示 hide為隱藏，mute為切換自訂顯示模式 可在muted\_color控制顏色， muted\_alpha控制顏色濃淡

## 程式範例

顯示程式碼

```
 
import bokeh.io
from bokeh.resources import INLINE
from bokeh.models import HoverTool
from bokeh.palettes import Spectral4
from bokeh.plotting import figure, output_file, show, output_notebook, ColumnDataSource
from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT
import pandas as pd
 
# env settings
bokeh.io.reset_output()
bokeh.io.output_notebook(INLINE)
 
 
# set hover
## HoverTool
# 游標滑過時顯示資料,date格式需要轉換，不然會是timestamp
hover = HoverTool(
    tooltips = [
        ("date", "@date"),
        ("close", "@open"),
        ("close", "@close"),
        ("high", "@high"),
        ("low", "@low"),
        ("volume","@volume")
    ], 
    formatters={"@date":"datetime"}
)
 
# set figure
p = figure(
    plot_width=1000, 
    plot_height=400, 
    x_axis_type="datetime",
    tools=[hover,"pan,box_zoom,reset,save"],
)
p.title.text = 'Stock_Price--Click on legend entries to mute the corresponding lines and show daily details in hover'
 
# use ColumnDataSource to control
# click_policy
# 藉由標籤控制數值顯示
# hide為隱藏，mute為切換自訂顯示模式
# 可在muted_color控制顏色, muted_alpha控制顏色濃淡
 
for data, name, color in zip([AAPL, IBM, MSFT, GOOG], ["AAPL", "IBM", "MSFT", "GOOG"], Spectral4):
    df = pd.DataFrame(data)
    df['date'] = pd.to_datetime(df['date'])
    source = ColumnDataSource(df)
    p.line(x="date",y="close", line_width=2, color=color, alpha=0.8,
           muted_color=color, muted_alpha=0.2, legend_label=name,source=source)
 
 
p.legend.location = "top_left"
# use hide or mute
p.legend.click_policy="mute"
 
# output_file("interactive_legend.html", title="interactive_legend.py example")
 
show(p)
output_notebook() 
```

## 兩種模式結果比較

1. Mute

[![Bokeh Click policy mute 模式股價圖結果](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/xkQhfUt-1024x445.png)](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/xkQhfUt-1024x445.png)

```
   2.  Hide
```

[![Bokeh Click policy hide 模式股價圖結果](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/fKmVrH1-1-1024x456.png)](https://r2.finlab.finance/blog/python-bokeh1-setup-and-first-impression/fKmVrH1-1-1024x456.png)

## 小結

這篇主要是來體會一下Bokeh的效果，不用會JS，隨便玩就有這種效果，蠻滿意的，值得再深入一下細節，把以前matplotlib的圖表都轉來bokeh，無論是在工作上的資料報告還是看盤需要，都蠻方便的。

假如想要進一步學習 Bokeh，我將會分享一系列文章，來介紹 Bokeh 在投資領域的視覺化喔！ [下一篇文章，帶你繪製專業的股價和技術指標](https://finlab.finance/blog/bokeh-custom-technical-chart)！

## 程式檔案連結

<https://github.com/benbilly3/bokeh_explore>

***

## 延伸閱讀

* [Plotly & Dash：用 Python 製作已實現損益儀表板](https://finlab.finance/blog/plotly-dash-realized-pnl-dashboard)
* [1 分鐘學會！用 Lux API 自動視覺化 Pandas 資料](https://finlab.finance/blog/lux-api-pandas-auto-visualization)
* [Bokeh 探索頻道(2)：客製化技術分析互動圖表](https://finlab.finance/blog/bokeh-custom-technical-chart)
* [程式交易是什麼？完整入門指南](https://finlab.finance/tools/program-trading)

**投資警語：本文僅供教學參考，不構成投資建議。過去績效不代表未來表現，投資有風險。**

![韓承佑](https://finlab.finance/people/cheng-yu-han-avatar.jpg)

韓承佑

創辦人 & 量化研究員

韓承佑是 FinLab 創辦人，也是開源 Python 量化套件 finlab 的作者，長年投入台股與美股的量化策略研究、回測與教學。

[在 GitHub 上追蹤 →](https://github.com/koreal6803) [在 LinkedIn 上連結 →](https://www.linkedin.com/in/cheng-yu-han/)

FinLab AI

### 想建立自己的策略？

用自然語言描述你的選股想法，AI 自動驗證、回測、給你答案

[免費開始](https://finlab.finance/#get-started)

## 更多AI 與程式研究

[查看全部](https://finlab.finance/blog/category/ai)

* [![AI 選股提示詞 30 條與真實回測：熱門提示詞為何失效，有效寫法長什麼樣](https://r2.finlab.finance/blog/ai-stock-picking-prompts-backtest/chart_equity_all.png) AI 選股提示詞 30 條與真實回測：熱門提示詞為何失效，有效寫法長什麼樣 年化報酬 30.9% · 台股](https://finlab.finance/blog/ai-stock-picking-prompts-backtest)
* [![Claude Code 量化交易實戰：裝 finlab 跑台股籌碼回測，從一句話到三因子迭代](https://r2.finlab.finance/blog/claude-code-finlab-quant-trading/chart_equity_2026-10-10.png) Claude Code 量化交易實戰：裝 finlab 跑台股籌碼回測，從一句話到三因子迭代 年化報酬 18.9% · 台股](https://finlab.finance/blog/claude-code-finlab-quant-trading)
* [![AI Agent 量化交易工具比較：Claude Code、Codex、Pi、Hermes、龍蝦怎麼選](https://r2.finlab.finance/blog/ai-agent-quant-trading-tools/chart_equity_2026-10-11.png) AI Agent 量化交易工具比較：Claude Code、Codex、Pi、Hermes、龍蝦怎麼選 年化報酬 18.3% · 台股](https://finlab.finance/blog/ai-agent-quant-trading-tools)
* [![AI 量化實測：Qwen 3 自動寫策略，12 次迭代結果](https://r2.finlab.finance/blog/qwen3-quant-assistant/chart_validation_2026-10-30.png) AI 量化實測：Qwen 3 自動寫策略，12 次迭代結果 年化報酬 14.0% · 台股](https://finlab.finance/blog/qwen3-quant-assistant)

來源：https://finlab.finance/blog/python-bokeh-interactive-charts-intro｜資料日期：依上文各表格與段落標示｜頁面更新：2026-11-25
