ftp.nice.ch/pub/next/unix/developer/plplot.3.0.s.tar.gz#/plplot/src/plhist.c

This is plhist.c in view mode; [Download] [Up]

/* Draws a histogram of n values of a variable in array data[0..n-1] */
/* in the range datmin to datmax using nbin bins. If "oldwin" is   */
/* 1, the histogram is plotted in the current window. If not,   */
/* the routine calls "plenv" to set up the graphics environment.   */

#include "plplot.h"
#include <math.h>
#if defined(PLSTDC) && defined(STDLIB)
#include <stdlib.h>
#else
extern char *malloc();
extern void free();
#endif

void plhist(n,data,datmin,datmax,nbin,oldwin)
int n, nbin, oldwin;
double datmin, datmax;
FLOAT *data;
{
  int bin, level;
  double dx, ymax;
  FLOAT *x, *y;
  short i;

  glev(&level);
  if (level<1) plexit("Please call plstar before plhist.");
  if (level<3 && oldwin)
          plexit("Please set up window before calling plhist.");
  if (datmin >= datmax)
          plexit("Data range invalid in plhist.");

  if(!(x = (FLOAT *)malloc(nbin*sizeof(FLOAT))))
     plexit("Out of memory in plhist");
  if(!(y = (FLOAT *)malloc(nbin*sizeof(FLOAT))))
     plexit("Out of memory in plhist");

  dx = (datmax-datmin) / nbin;
  for (i=0; i<nbin; i++) {
    x[i] = datmin + i*dx;
    y[i] = 0.0;
  }

  for (i=0; i<n; i++)  {
    bin = (data[i] - datmin)/dx;
    bin = bin > 0 ? bin : 0;
    bin = bin < nbin ? bin : nbin-1;
    y[bin]++;
  }

  if (!oldwin) {
    ymax = 0.0;
    for (i=0; i<nbin; i++) ymax = MAX(ymax,y[i]);
    plenv(datmin,datmax,0.0,1.1*ymax,0,0);
  }

  plbin(nbin,x,y,0);
  free((VOID *)x);
  free((VOID *)y);
}


These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.