//Subdivides a population in 5 bins and creates a boxplot with one box per bin. //Here, the whiskers indicate the min and max values //Norbert Vischer, 27.01.2018 02:56 macro "Demo BoxPlots"{ //1. create some demo data: len = 200; newImage("Boxes Demo", "8-bit ramp", len, 100, 1); random("seed", 0); run("Add Specified Noise...", "standard=150"); run("Select All"); yy = getProfile; close("Boxes Demo"); xx = Array.getSequence(len); //2. process demo data in arrays xx and yy nBins = 5; xCenter = newArray(nBins); y1 = newArray(nBins);//bottom 1st quartile (min) y2 = newArray(nBins);//bottom 2nd quartile y3 = newArray(nBins);//bottom 3rd quartile (median) y4 = newArray(nBins);//bottom 4th quartile y5 = newArray(nBins);//top 4th quartile (max) for(bin = 0; bin < nBins; bin++){ chunk = len/nBins; left = bin * chunk; right = left + chunk; xCenter[bin] = (left + right)/2; subXX = Array.slice(xx, left, right); subYY = Array.slice(yy, left, right); sublen = subYY.length; Array.sort(subYY); y1[bin] = subYY[0]; y2[bin] = subYY[sublen * 0.25]; y3[bin] = subYY[sublen * 0.5]; y4[bin] = subYY[sublen * 0.75]; y5[bin] = subYY[sublen-1]; } //3. Draw the plot Plot.create("Boxes Demo", "X", "y"); Plot.setLimits(0, 200, 50, 180); Plot.setLineWidth(5); Plot.setColor("#88cc88"); Plot.add("line", xCenter, y3); Plot.setColor("#00aa00"); Plot.setLineWidth(1); Plot.add("circles", xx, yy); Plot.setLineWidth(2); Plot.setColor("blue", "#ccccff"); boxWidth = 22; Plot.drawBoxes(boxWidth, xCenter, y1, y2, y3, y4, y5); //Plot.drawBoxes(boxWidth, xCenter, y2, y2, y3, y4, y4);// =omit whiskers Plot.setLineWidth(1); Plot.show; }