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
廣告