Plotlib

Basic (non-X) Example

The following example illustrates basic usage with a Plotlib-created X window. The following code creates two plots within a single plot window:


#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include "plotlib_server.h"


main(int, char *[])
{
    PlotWindow *window;
    HistogramPlot *histogramplot;
    int i,j;
    static float data[10000];
    static float bins[21] = { 0 };

	/* data */

    for (i=0;i < 10000;i++) {
	float x1 = ((double)random() / INT_MAX) * 10;
	float x2 = ((double)random() / INT_MAX) * 10;
	data[i] = x1+x2;
	bins[(int)(x1+x2+0.5)]++;
    }

	/* create plot window */

    window = new PlotWindow();
    if (!window || window->failed) {
	if (!window)
	    (void)fprintf(stderr, "Insufficient memory\n");
	return 1;
    }
    window->set_autoscale();
    window->set_axes_color(BLACK);
    window->set_window_height_and_width(200,200);
    window->set_axes_labels("Sum","Count");
    window->set_title("Histogram Test");
    window->set_font_type("helvetica");
    window->finish();

	/* create plot */

    histogramplot = new HistogramPlot(window, bins, 21, RED, 0);
    if (!histogramplot || histogramplot->failed) {
	if (!histogramplot) (void)fprintf(stderr, "Insufficient memory\n");
	return 1;
    }
    histogramplot = new HistogramPlot(window, data, 10000, BLACK, 50);
    if (!histogramplot || histogramplot->failed) {
	if (!histogramplot) (void)fprintf(stderr, "Insufficient memory\n");
	return 1;
    }

	/* prompt for change */

    (void)printf("Hit RETURN for next plot window\n");
    getchar();
    window->set_window_height_and_width(300,300);

	/* wait for user to respond */

    (void)printf("Hit RETURN for next plot window\n");
    getchar();
    delete window;

    return 0;
}