Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การสร้าง Color Scatter Plot แบบ Interactive ด้วย Bokeh


บทนำ

Bokeh: แพ็กเกจ Visualization โค้ดเปิด ใช้ HTML/JavaScript เหมาะกับ Web Dashboard ใช้กับ NumPy, Pandas ได้ สร้างกราฟโต้ตอบ (Interactive) ต่างจาก Matplotlib/Seaborn ที่เป็น Static สามารถฝังใน Flask/Django/Jupyter ได้

Dependencies & Installation

Numpy, Pillow, Jinja2, Packaging, Pyyaml, Six, Tornado, Python-dateutil
pip3 install bokeh
conda install bokeh

ตัวอย่าง: Color Scatter Plot พร้อม Hover Tool

import numpy as np
from bokeh.plotting import figure, output_file, show

N = 5000
x = np.random.random(size=N) * 125
y = np.random.random(size=N) * 125
radii = np.random.random(size=N) * 1.35

colors = [
    "#%02x%02x%02x" % (int(r), int(g), 150)
    for r, g in zip(40+2*x, 30+2*y)
]

TOOLS = "hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select"

p = figure(tools=TOOLS)
p.scatter(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)

output_file("colorscatter.html", title="Color Scatter Plot")
show(p)

ผลลัพธ์

การสร้าง Color Scatter Plot แบบ Interactive ด้วย Bokeh

คำอธิบาย

  • นำเข้าแพ็กเกจและตั้งชื่อย่อ
  • สร้างข้อมูลสุ่ม 5,000 จุด
  • กำหนดสีแบบไดนามิกจากพิกัด x,y
  • กำหนด TOOLS รวม hover tool
  • ใช้ scatter วาดจุด กำหนด radius, fill_color, alpha
  • บันทึกเป็น HTML และแสดงผล