使用 Python Matplotlib 繪製箱線圖時如何處理 NaN 值?
可以使用以下步驟來處理 NaN 值,同時使用 Python 繪製箱線圖 −
步驟
設定數字大小並調整子圖之間以及子圖周圍的填充。
初始化用於資料樣本和範圍的變數**N**。
接下來,建立隨機分佈、中心資料、最高離群值和最低離群值,獲取連線資料以及過濾資料。
使用**boxplot()**方法建立箱線圖。
要顯示數字,請使用**show()**方法。
示例
import matplotlib.pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Data samples N = 10 # Random spread spread = np.random.rand(N) # Center's data center = np.ones(N) # Flier high and low fh = np.random.rand(N)+N fl = np.random.rand(N)-N # Concatenated data data = np.concatenate((spread, center, fh, fl), 0) data[5] = np.NaN # Filtered data filtered_data = data[~np.isnan(data)] # Plot the boxplot plt.boxplot(filtered_data) plt.show()
輸出
將產生以下輸出 −
廣告