2017-03-16 235 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]