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

Vẽ error bars từ DataFrame dùng Seaborn FacetGrid (Matplotlib)


Dùng sns.FacetGrid kết hợp plt.errorbar để vẽ error bars cho từng nhóm dữ liệu trong DataFrame.

  • Tạo DataFrame với cột giá trị và độ lỗi.
  • Tạo FacetGrid theo cột nhóm.
  • Map plt.errorbar với tham số yerr.

Ví dụ minh họa

import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

df = pd.DataFrame({'col1': [3.0, 7.0, 8.0],
                   'col2': [1.0, 4.0, 3.0]})

g = sns.FacetGrid(df, col="col1", hue="col1")
g.map(plt.errorbar, "col1", "col2", yerr=0.75, fmt='o')
plt.show()

Kết quả đầu ra

Vẽ error bars từ DataFrame dùng Seaborn FacetGrid (Matplotlib)