Matplotlib - 小提琴图



小提琴图类似于箱线图,不同之处在于它们还显示了数据在不同值处的概率密度。这些图包括数据中位数的标记以及指示四分位距的框,就像标准箱线图一样。在此箱线图上叠加了核密度估计。与箱线图一样,小提琴图用于表示跨不同“类别”的变量分布(或样本分布)的比较。

小提琴图比简单的箱线图更具信息量。事实上,虽然箱线图仅显示摘要统计信息,例如均值/中位数和四分位距范围,但小提琴图显示了数据的完整分布。

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(10)
collectn_1 = np.random.normal(100, 10, 200)
collectn_2 = np.random.normal(80, 30, 200)
collectn_3 = np.random.normal(90, 20, 200)
collectn_4 = np.random.normal(70, 25, 200)

## combine these different collections into a list
data_to_plot = [collectn_1, collectn_2, collectn_3, collectn_4]

# Create a figure instance
fig = plt.figure()

# Create an axes instance
ax = fig.add_axes([0,0,1,1])

# Create the boxplot
bp = ax.violinplot(data_to_plot)
plt.show()
Violin Plot
广告

© . All rights reserved.