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

วิธีพลอตหลายรูปทรงบนกราฟเดียวด้วย Bokeh ใน Python

บทนำ

Bokeh รองรับการวาดรูปทรงเรขาคณิตหลากหลายบน Figure เดียว เช่น สี่เหลี่ยม, สี่เหลี่ยมจัตุรัส, วงรี, รูปไข่

การติดตั้ง

pip3 install bokeh
conda install bokeh

Dependencies

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

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

from bokeh.plotting import figure, output_file, show

my_fig = figure(plot_width=400, plot_height=300)
my_fig.rect(x=11, y=11, width=150, height=75, width_units='screen', height_units='screen')
my_fig.square(x=2, y=3, size=80, color='blue')
my_fig.ellipse(x=7, y=6, width=30, height=10, fill_color=None, line_width=2)
my_fig.oval(x=6, y=6, width=2, height=1, angle=-0.4)
show(my_fig)

คำอธิบาย

  • ใช้ rect สำหรับสี่เหลี่ยมผืนผ้า (ระบุหน่วย screen)

  • ใช้ square สำหรับสี่เหลี่ยมจัตุรัส

  • ใช้ ellipse และ oval สำหรับวงรี/ไข่ พร้อมพารามิเตอร์การหมุน