1 回答

TA貢獻1802條經(jīng)驗 獲得超5個贊
考慮在 matplotlib 中具有多個直方圖的圖,如下所示:
#! /usr/bin/env python3
import matplotlib.pyplot as plt
import random
# Use the same seed for reproducibility.
random.seed(10586)
data1 = [random.gauss(1e-4, 3e-2) for _ in range(10**3)] + [0.3]
data2 = [random.gauss(1e-2, 3e-3) for _ in range(10**3)] + [0.4]
data3 = [0.2]
if __name__ == '__main__':
plt.xlim(xmin=0, xmax=0.8)
plt.yscale('log')
n1, bins1, patches1 = plt.hist(data1, bins='auto', alpha=0.6)
n2, bins2, patches2 = plt.hist(data2, bins='auto', alpha=0.6)
n3, bins3, patches3 = plt.hist(data3, bins='auto', alpha=0.6)
bin_options = ['auto', 'fd', 'doane', 'scott', 'rice', 'sturges', 'sqrt']
plt.show()
然而,第三個數(shù)據(jù)集只有一個數(shù)據(jù)點,所以當(dāng)我們使用時,plt.hist(data3, bins='auto') 我們得到一個橫跨 x 范圍的長條,并且看不到它的值為 0.2:
或者histtype="stepfilled"
用來創(chuàng)建一個多邊形,因為無論如何單個條形都無法與那么多垃圾箱區(qū)分開來,
n2, bins2, patches2 = plt.hist(data2, bins='auto', alpha=0.6, histtype="stepfilled")
后者還具有服從 alpha 的優(yōu)點,否則由于條形的重疊而看不到它。此外,它應(yīng)該更快地繪制單個形狀而不是大約 1000 個條形圖。
添加回答
舉報