Computer >> Máy Tính >  >> Lập trình >> Python

Làm thế nào để lấy dữ liệu boxplot cho các boxplot Matplotlib?

Để nhận dữ liệu về ô hộp cho ô ô Matplotlib, chúng ta có thể thực hiện các bước sau -

  • Đặt kích thước hình và điều chỉnh phần đệm giữa và xung quanh các ô phụ.
  • Tạo khung dữ liệu bằng gấu trúc.
  • Tạo một biểu đồ hộp từ các cột DataFrame.
  • Nhận ngoại lệ của boxplot , hộp , trung bình râu dữ liệu.
  • Tổng hợp tất cả các thông tin trên.
  • Để hiển thị hình này, hãy sử dụng show () phương pháp.

Ví dụ

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

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame(dict(age=[23, 45, 21, 15, 12]))
_, bp = pd.DataFrame.boxplot(df, return_type='both')
outliers = [flier.get_ydata() for flier in bp["fliers"]]
boxes = [box.get_ydata() for box in bp["boxes"]]
medians = [median.get_ydata() for median in bp["medians"]]
whiskers = [whiskers.get_ydata() for whiskers in bp["whiskers"]]

print("Outliers: ", outliers)
print("Boxes: ", boxes)
print("Medians: ", medians)
print("Whiskers: ", whiskers)

plt.show()

Đầu ra

Outliers: [array([45])]
Boxes: [array([15., 15., 23., 23., 15.])]
Medians: [array([21., 21.])]
Whiskers: [array([15., 12.]), array([23., 23.])]

Làm thế nào để lấy dữ liệu boxplot cho các boxplot Matplotlib?