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

Cách chỉnh sửa thuộc tính whiskers, fliers, caps, v.v. trong Seaborn boxplot Matplotlib?


Để chỉnh sửa thuộc tính whiskers, fliers, caps, v.v. trong Seaborn boxplot, chúng ta có thể thực hiện các bước sau:

  • Thiết lập kích thước figure và điều chỉnh padding giữa và xung quanh subplots.
  • Tạo dataframe bằng Pandas.
  • Vẽ boxplot từ cột DataFrame.
  • Lấy dữ liệu outliers, boxes, medians và whiskers của boxplot.
  • In tất cả dữ liệu trên.
  • Để hiển thị figure, dùng phương thức 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 = [whiskers.get_ydata() for whiskers in bp["whiskers"]]

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

plt.show()

Kết quả

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

Cách chỉnh sửa thuộc tính whiskers, fliers, caps, v.v. trong Seaborn boxplot Matplotlib?