Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Trực quan hóa nhiều đường (multi-line) với Bokeh Python


Bokeh: package Python trực quan hóa, mã nguồn mở, render HTML/JS → dashboard web.

Bokeh chuyển data source thành JSON, input cho BokehJS (TypeScript) render trên browser hiện đại.

Bokeh tạo biểu đồ tương tác (interactive) khác Matplotlib/Seaborn (static). Nhúng Flask/Django/Jupyter.

Dependencies: NumPy, Pillow, Jinja2, Packaging, PyYAML, Six, Tornado, python-dateutil.

Cài đặt: pip3 install bokeh | conda install bokeh

Ví dụ

from bokeh.plotting import figure, output_file, show

output_file("patchplot.html")
p = figure(plot_width=500, plot_height=300)
p.multi_line([[1, 2, 2], [3, 2, 5, 8]], [[2, 3, 7], [4, 8, 9, 0]],
             color=["firebrick", "cyan"], alpha=[0.9, 0.3], line_width=4)
show(p)

Đầu ra

Trực quan hóa nhiều đường (multi-line) với Bokeh Python

Giải thích

  • Import Bokeh plotting.
  • figure() width/height.
  • output_file() file HTML.
  • multi_line() vẽ nhiều đường: xs=[[1,2,2],[3,2,5,8]], ys=[[2,3,7],[4,8,9,0]], màu/alpha/width riêng từng đường.
  • show() hiển thị.