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

วิธีสร้างกราฟคลื่นไซน์ (Sinusoidal Wave) ด้วย Bokeh ใน Python

บทนำ

Bokeh เหมาะสำหรับพลอตฟังก์ชันคณิตศาสตร์แบบโต้ตอบ รองรับ NumPy สำหรับสร้างข้อมูล

การติดตั้ง

pip3 install bokeh
conda install bokeh

Dependencies

Numpy, Pillow, Jinja2, Packaging, Pyyaml, Six, Tornado, Python-dateutil

ตัวอย่างโค้ด

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

x = np.arange(0, math.pi * 4, 0.1)
y = np.sin(x)

output_file("sample.html")
p = figure(title="A simple sine wave", x_axis_label='x', y_axis_label='y')
p.line(x, y, legend_label="sine", line_width=2)
show(p)

คำอธิบาย

  • สร้าง x ด้วย np.arange ครอบ 4π

  • คำนวณ y = sin(x)

  • ใช้ line พร้อม legend และ line_width