http://bokeh.pydata.org/en/latest
Bokeh
是一個專門針對Web瀏覽器的呈現功能的交互式可視化Python庫。這是Bokeh與其它可視化庫最核心的區別。
-
專門針對Web瀏覽器的交互式、可視化Python繪圖庫
-
可以做出像D3.js簡潔漂亮的交互可視化效果,但是使用難度低於D3.js。
-
獨立的HTML文檔或服務端程序
-
可以處理大量、動態或數據流
-
支持Python (或Scala, R, Julia…)
-
不需要使用Javascript
Bokeh接口
-
Charts: 高層接口,以簡單的方式繪制復雜的統計圖
-
Plotting: 中層接口,用於組裝圖形元素
-
Models: 底層接口,為開發者提供了最大的靈活性
包引用
from bokeh.io import output_notebook, output_file, show from bokeh.charts import Scatter, Bar, BoxPlot, Chord from bokeh.layouts import row import seaborn as sns # 導入數據 exercise = sns.load_dataset('exercise') output_notebook() #output_file('test.html')
-
from bokeh.io import output_file
生成.html文檔 -
from boken.io import output_notebook
在jupyter中使用
bokeh.charts
http://bokeh.pydata.org/en/latest/docs/reference/charts.html
散點圖 Scatter
示例代碼:
# 散點圖 p = Scatter(data=exercise, x='id', y='pulse', title='exercise dataset') show(p)
運行結果:
柱狀圖 Bar
示例代碼:
# 柱狀圖 p = Bar(data=exercise, values='pulse', label='diet', stack='kind', title='exercise dataset') show(p)
運行結果:
盒子圖 BoxPlot
示例代碼:
# 盒子圖 box1 = BoxPlot(data=exercise, values='pulse', label='diet', color='diet', title='exercise dataset') box2 = BoxPlot(data=exercise, values='pulse', label='diet', stack='kind', color='kind', title='exercise dataset') show(row(box1, box2))
運行結果:
弦圖 Chord
• 展示多個節點之間的聯系
• 連線的粗細代表權重
示例代碼:
# 弦圖 Chord chord1 = Chord(data=exercise, source="id", target="kind") chord2 = Chord(data=exercise, source="id", target="kind", value="pulse") show(row(chord1, chord2))
運行結果:
bokeh.plotting
方框 square, 圓形 circle
示例代碼:
from bokeh.plotting import figure import numpy as np p = figure(plot_width=400, plot_height=400) # 方框 p.square(np.random.randint(1,10,5), np.random.randint(1,10,5), size=20, color="navy") # 圓形 p.circle(np.random.randint(1,10,5), np.random.randint(1,10,5), size=10, color="green") show(p)
運行結果:
更多圖形元素參考:http://bokeh.pydata.org/en/latest/docs/reference/plotting.html