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

Tạo biểu đồ phân tán (scatter plot) với Bokeh trong Python

Bokeh vẽ scatter plot tương tác, hỗ trợ zoom, pan, hover, export PNG.

Cài đặt

pip3 install bokeh
conda install bokeh

Ví dụ cơ bản


from bokeh.plotting import figure, output_file, show

fig = figure(plot_width=500, plot_height=400)
fig.scatter([1, 3, 7, 5, 4, 9], [6, 5, 9, 8, 0, 1], marker="circle", size=20, fill_color="grey")
output_file('scatterplot.html')
show(fig)

Kết quả

Tạo biểu đồ phân tán (scatter plot) với Bokeh trong Python

Giải thích

  • scatter() vẽ điểm, tham số marker, size, fill_color.
  • Có thể truyền source=ColumnDataSource(df) để vẽ từ DataFrame.

Mở rộng: Thêm HoverTool, thay đổi marker theo nhóm dữ liệu, thêm đường hồi quy.