【发布时间】:2016-08-29 08:58:29
【问题描述】:
我是 matplotlib 的新手,正在尝试绘制直方图。我对较低的 bin 范围感兴趣,因此拆分了我的 bin 范围,但它看起来有点难看,右侧有很多空白。 我有一些代码可以生成这个直方图,但我想改变它:
- 将 xaxis 位置 150 之后的所有条组合为 150+,以便更好地显示较低范围的条。
- 改变条的颜色
- 最左边的栏换成不同的颜色
- 代表 x 轴刻度在 5-40 之间的条具有不同的颜色
-
来自 40 多种不同颜色的条形
import matplotlib matplotlib.use('PS') import matplotlib.pyplot as plt # sample data, These are not actual values since I have a large csv file # with 1000's of rows. 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)) n, bins, patches=plt.hist(values, bins = binr) one, fifty = np.percentile(values, [0.5,50]) for patch, rightside, leftside in zip(patches, bins[1:], bins[:-1]): if rightside < one: patch.set_facecolor('green') elif leftside > fifty: patch.set_facecolor('red') plt.title("Frequency Histogram") plt.xlabel("Word Count") plt.ylabel("Frequency") plt.savefig(plot_file) plt.close()
【问题讨论】:
-
感谢(点赞)制作了一个小例子。
标签: python numpy matplotlib histogram