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

Vẽ nhiều hình dạng (shapes) trên một biểu đồ Bokeh trong Python

Bokeh cung cấp các glyph hình học: rect, square, ellipse, oval... để vẽ trực tiếp hình dạng lên figure.

Cài đặt

pip3 install bokeh
conda install bokeh

Ví dụ


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)

Kết quả

Vẽ nhiều hình dạng (shapes) trên một biểu đồ Bokeh trong Python

Giải thích

  • rect với đơn vị screen (pixel) thay vì data units.
  • square vuông, ellipse elip, oval oval có góc xoay.
  • Có thể kết hợp với dữ liệu thực để chú thích biểu đồ.