2017-03-16 106 views

回答

4

當致電plt.hist()時,它將返回三件事情。首先是一個數組,它包含每個bin中的值。其次是每個bin的值,最後是一個patches的數組。這些讓你可以單獨修改每個小節。因此,所有你需要做的是確定哪是範圍130-132,然後修改顏色,例如:

import numpy as np 
import matplotlib.pyplot as plt 

values = np.random.randint(51, 140, 1000) 
n, bins, patches = plt.hist(values, bins=np.arange(50, 140, 2), align='left', color='g') 
patches[40].set_fc('r') 
plt.show() 

會顯示類似:

example showing one red bar

這裏的第41補丁對應範圍130-132作爲我選擇的箱子開始於50並且以2爲步驟上升到140。因此總共將有45個分箱。如果你print bins你會看到索引40是你想要的:

[ 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 
    86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 
122 124 126 128 130 132 134 136 138]