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

Xử lý giá trị NaN khi vẽ boxplot bằng Python Matplotlib


Để xử lý giá trị NaN khi vẽ boxplot bằng Python, thực hiện các bước sau:

Các bước

  • Thiết lập kích thước hình và padding.
  • Khởi tạo biến N cho mẫu dữ liệu và range.
  • Tạo random spread, center, flier high/low, nối dữ liệu, lọc bỏ NaN.
  • Vẽ boxplot bằng boxplot().
  • Hiển thị hình bằng show().

Ví dụ

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
N = 10
spread = np.random.rand(N)
center = np.ones(N)
fh = np.random.rand(N) + N
fl = np.random.rand(N) - N
data = np.concatenate((spread, center, fh, fl), 0)
data[5] = np.NaN
filtered_data = data[~np.isnan(data)]
plt.boxplot(filtered_data)
plt.show()

Kết quả

Boxplot vẽ từ dữ liệu đã lọc bỏ NaN, hiển thị quartile, median, whiskers, outliers.