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

Lấy dữ liệu boxplot (outliers, boxes, medians, whiskers) trong Matplotlib


Các bước

  1. Thiết lập kích thước hình và padding.
  2. Tạo DataFrame Pandas.
  3. Vẽ boxplot với return_type='both'.
  4. Trích xuất dữ liệu: outliers, boxes, medians, whiskers.
  5. In thông tin.
  6. Gọi show().

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 = [whisker.get_ydata() for whisker in bp["whiskers"]]

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

Kết quả

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

Lấy dữ liệu boxplot (outliers, boxes, medians, whiskers) trong Matplotlib