如何获取 Matplotlib 箱型图的箱型图数据?
要获取 Matplotlib 箱型图的箱型图数据,我们可以执行以下步骤:
- 设置图形大小和调整子图之间及周围的填充。
- 使用 pandas 制作数据帧。
- 从 DataFrame 列制作箱型图。
- 获取箱型图的离群值、箱子、中位数和须毛数据。
- 打印上述所有信息。
- 要显示图形,请使用show()方法。
示例
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()输出
Outliers: [array([45])] Boxes: [array([15., 15., 23., 23., 15.])] Medians: [array([21., 21.])] Whiskers: [array([15., 12.]), array([23., 23.])]

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP