我不太清楚你要做什么,尤其是你的期望似乎与你的简单例子有点矛盾(我是指基于百分位的着色)。在
无论如何,我建议您直接使用np.histogram(因为您已经导入了numpy),并手动调用{}。这样做的主要优点(除了可以更好地控制输出,以稍微增加一些工作量为代价)是您可以传递一个包含每个条颜色的列表。在
示例的修改版本:values=[1,1,1,1,1,1,1,2,2,2,2,4,4,4,5,6,7,8,9,10,111,12,23,30,30,35,353,35,25,25,25,15,15,15,20,20,20,40,40,40,45,50,55,50,50,100,200,300,400]
limit1, limit2 = 50, 500
binwidth1, binwidth2 = 5, 100
binr=list(range(0, limit1, binwidth1)) + list(range(100, limit2, binwidth2))
# improvement 1: merge bins above 150, keep the same maximum
thresh = 150
# keep the first value after the threshold too
binr_tmp = [val for val in binr if val<=thresh]
binr = binr_tmp + [binr[len(binr_tmp)], binr[-1]]
# improvement 2: use np.histogram explicitly, feed into plt.bar later (for colors)
bin_vals, bins = np.histogram(values,