void plplot_plot_hist( plot_driver_type * driver, const char * label , double_vector_type * x , line_attribute_type line_attr) { int size = double_vector_size( x ); plplot_setup_linestyle( line_attr ); { int bins = (int) sqrt( size ); double xmin = double_vector_get_min( x ); double xmax = double_vector_get_max( x ); { /* Could for some fuxxxing reason not get the plhist() function to work, and had to resort to the low level plbin function. */ double * limits = util_calloc(bins + 1 , sizeof * limits ); double * x_ = util_calloc(bins , sizeof * x_ ); double * y_ = util_calloc(bins , sizeof * y_ ); int i; double delta = (xmax - xmin) / bins; for (i= 0; i <= bins; i++) limits[i] = xmin + i*delta; for (i=0; i < bins; i++) { y_[i] = 0; x_[i] = 0.50 * (limits[i] + limits[i + 1]); } for (i=0; i < size; i++) { double value = double_vector_iget(x , i); int j; for (j = 1; j <= bins; j++) if (value < limits[j]) { y_[j-1]++; break; } } /* for (i = 0; i < bins; i++) printf("x[%d] = %g y[%d] = %g\n",i,x_[i],i,y_[i]); */ plbin(bins , x_ , y_ , PL_BIN_CENTRED + PL_BIN_NOEXPAND); free(x_); free(y_); free(limits); } //plhist(size , double_vector_get_ptr(d->x) , xmin , xmax , bins , 0 /* PL_HIST_DEFAULT */); } }
/* * Plot a histogram of prebinned data. X values are int32_t's, and the corresponding * Y values - the counts for each X - are uint32_t's. */ int plplotBins( uint32_t numBins, const int32_t *x, /* numBins of X values, monotonically increasing */ const uint32_t *y, /* numBins of Y values for each associated X */ const char *graphName, const char *fileName, const char *xAxisName, /* optional */ const char *yAxisName) /* optional */ { char tmpFile[TEMP_FN_LEN]; makeTempFile(tmpFile, TEMP_FN_LEN); PLINT totalBins = numBins + 2; /* PLFLT array of sample values */ PLFLT *xf = (PLFLT *)malloc(totalBins * sizeof(PLFLT)); /* these two will have Y values of zero */ xf[0] = x[0] - 1; xf[totalBins - 1] = x[numBins-1] + 1; const int32_t *ip = x; PLFLT *op = xf + 1; for(uint32_t dex=0; dex<numBins; dex++) { *op++ = *ip++; } /* PLFLT array of bins */ PLFLT *yf = (PLFLT *)malloc(totalBins * sizeof(PLFLT)); yf[0] = 0.0; yf[totalBins - 1] = 0.0; const uint32_t *uip = y; op = yf + 1; for(uint32_t dex=0; dex<numBins; dex++) { *op++ = *uip++; } /* get max Y value */ uint32_t maxY = 0; uip = y; for(uint32_t dex=0; dex<numBins; dex++) { uint32_t currY = *uip++; if(currY > maxY) { maxY = currY; } } const char *yName = yAxisName ? yAxisName : ""; const char *xName = xAxisName ? xAxisName : ""; #if HIST_COLOR plsdev ("psc"); plscolbg(255, 255, 255); /* white background */ #else plsdev ("ps"); #endif plsfnam(tmpFile); plsdiori(1.0); // portrait plinit(); plenv(xf[0], xf[totalBins - 1], 0, maxY, 0, 0); #if HIST_COLOR /* can we alter colors of lines and the spaces inside the histograms? */ plscolbg(255, 0, 0); /* red background */ plscol0(1, 255, 0, 0); /* red foreground - no effect */ #endif pllab(xName, yName, graphName); plbin(totalBins, xf, yf, PL_BIN_CENTRED); plend(); free(xf); free(yf); int ourRtn = psToPdf(tmpFile, fileName); unlink(tmpFile); return ourRtn; }
/* * Plot a histogram showing the number of occurences of each possible * value of 'samples'. */ int plplotHist( const int32_t *samples, uint32_t numSamples, const char *graphName, const char *fileName, const char *xAxisName, /* optional */ const char *yAxisName) /* optional */ { char tmpFile[TEMP_FN_LEN]; makeTempFile(tmpFile, TEMP_FN_LEN); /* First determine the range, i.e. the number of bins */ int32_t minSamp = samples[0]; int32_t maxSamp = samples[0]; for(uint32_t dex=0; dex<numSamples; dex++) { int32_t s = samples[dex]; if(s < minSamp) { minSamp = s; } if(s > maxSamp) { maxSamp = s; } } /* When we specify PL_BIN_CENTRED, the min and max values are half the normal width */ minSamp--; maxSamp++; PLINT numBins = maxSamp - minSamp + 1; /* One array containing the sample values, x */ PLFLT *x = (PLFLT *)malloc(numBins * sizeof(PLFLT)); int32_t binNum = minSamp; for(uint32_t dex=0; dex<(uint32_t)numBins; dex++) { x[dex] = binNum++; } /* Now make and fill the bins proper */ PLFLT *y = (PLFLT *)malloc(numBins * sizeof(PLFLT)); for(uint32_t dex=0; dex<(uint32_t)numBins; dex++) { y[dex] = 0; } PLFLT maxY = 0.0; for(uint32_t dex=0; dex<numSamples; dex++) { int32_t s = samples[dex]; PLFLT *yp = y + s - minSamp; *yp += 1.0; if(*yp > maxY) { maxY = *yp; } } const char *yName = yAxisName ? yAxisName : ""; const char *xName = xAxisName ? xAxisName : ""; #if HIST_COLOR plsdev ("psc"); plscolor(1); plscolbg(255, 255, 255); /* white background */ #else plsdev ("ps"); #endif plsfnam(tmpFile); plsdiori(1.0); // portrait plinit(); plenv(minSamp, maxSamp, 0, maxY, 0, 0); #if HIST_COLOR /* can we alter colors of lines and the spaces inside the histograms? */ plscolbg(255, 0, 0); /* red background */ plscol0(1, 255, 0, 0); /* red foreground - no effect */ #endif pllab(xName, yName, graphName); plbin(numBins, x, y, PL_BIN_CENTRED); plend(); free(x); free(y); int ourRtn = psToPdf(tmpFile, fileName); unlink(tmpFile); return ourRtn; }