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

การแสดง Multiple Lines ด้วย Bokeh ใน Python


บทนำ

Bokeh: แพ็กเกจ Visualization โค้ดเปิด ใช้ HTML/JavaScript เหมาะกับ Web Dashboard แปลงข้อมูลเป็น JSON สำหรับ BokehJS (TypeScript) รันบน Browser สร้างกราฟโต้ตอบ ต่างจาก Matplotlib/Seaborn ที่เป็น Static ฝังใน Flask/Django/Jupyter ได้

Dependencies & Installation

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

ตัวอย่าง: Multi Line Plot

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)

ผลลัพธ์

การแสดง Multiple Lines ด้วย Bokeh ใน Python

คำอธิบาย

  • นำเข้าแพ็กเกจและตั้งชื่อย่อ
  • สร้าง Figure กำหนดขนาด
  • output_file กำหนดไฟล์ HTML output
  • ใช้ multi_line วาดหลายเส้นพร้อมกัน ระบุ xs, ys, color, alpha, line_width
  • show แสดงผล