Don Kelly - 2025-01-29

Hi,
I have a project in C that runs real-time and uses gnuplot for real-time plotting. I want to replace gnuplot with plplot. I’ve written a test script (see below), but the only way I’ve been able to replot y data is to use plclear() and then redraw all the axes, labels, title, etc. Is there a cleaner way to just update the y data and replot rather than using wiping out everything with plclear? Any suggestions welcome, as I’m a newbie to plplot!! Thanks, Don

//------------------------------------------------------------------------------
//
// gcc plplotLive1.c -o plplotLive1 -I/usr/include/plplot -lplplot -lm
// ./plplotLive1
//------------------------------------------------------------------------------

// Include libraries

include <plplot.h></plplot.h>

include <unistd.h> // for sleep()</unistd.h>

include <math.h></math.h>

include <stdio.h></stdio.h>

// Defined parameters

define PI 3.14159265358979323846

define AMP 1.0

define FREQ 1.0

define SRATE 100

define DUR 1.0

define TINT 2*PI/SRATE

// Main thread
int main() {

// Define sin wave data size
int nsamples = (int)(SRATE * DUR);
double xdata[nsamples];
double ydata[nsamples];
double time;

// Create a default sine wave from 0 to 2PI
for (int j=0; j<nsamples; j++) {
time = (double)(j) / SRATE;
ydata[j] = AMP * sin(2 * PI * FREQ * time);
xdata[j] = j * TINT;
}

// Declare plplot variables
PLFLT x[100], y[100];
PLCHAR_VECTOR afgi;

// Initialize PLplot
plsdev("xcairo"); // or any other suitable device
plspage(0,0,800,600,0,0); // Define size of fig

plinit();
plenv(0, 2*PI, -1.5, 1.5, 0, 0); // Set up the plot window
pllab(0, 0, "Live Plot Example");

// Set up colors
plscolbg(16,16,16);
//plcol0(15); // white
plcol0(1); // red
//plcol0(0); // black

//for (int i=0; i<400; i++) {
while(1) {

// Update the y data for new plot
ydata[100-1] = ydata[0];
for (int k=0; k<100-1; k++) {
  ydata[k] = ydata[k+1];
}

// Clear the plot
plclear(); // Clears both axes and data

// Plot the data, axes, and labels (because plclear has cleared these).
plcol0(1); // red pen
plbox("abcgnst", 0.0, 0, "abcgnst", 0.0, 0); // create box
pllab(0,0,"Live Plot Example"); // labels
plcol0(15); // white pen
plwidth(2); // thicker pen width
plline(nsamples, xdata, ydata); // draw line plot
plwidth(1); // default pen width

// Flush required to re-display plot
plflush(); // Required to display plot

// Wait for a short period of time to simulate live plotting
//usleep(1000000); // 1s (1 Hz)
//usleep(10000); // 10ms (100 Hz)
usleep(100000); // 100ms (10 Hz)
//usleep(5000); // 5ms (200 Hz)

} // End of while

// End plotting
plspause(0); // Use this command to close plot window
plend(); // Tidy up plplot and end

// End of main
printf("Program complete.\n");
return 0;

} // End of main

Don

Don Kelly

LinkedIn: www.linkedin.com/in/kellydak