sTadek
sTadek

Reputation: 11

Unwanted flat graph while drawing live gnuplot as command from c++ code, using popen() method

I wanted to include in c++ code the graph updated live while more data is collected from pipe, this was using 'popen()' method. I wanted pure plot to draw data as they are calculated. Here in example below I wanted 'y=2*x' function. But gnuplot draws only flat line presenting last point (constant y value for all x data). This flat line just changes 'height' after plot refreshing, because of changing input from pipe.

my code is here:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main (int argc, char **argv)
  {
  
  FILE *fp = NULL;
  fp =popen ("gnuplot-x11 -persist", "w");
  
  int i =0;
  unsigned char x_data[100000],
                y_data[100000];
  
  
  for (i=0; i<=100; i++)
    {
    
    x_data[i]=i;
    y_data[i]=2*i;
  
    printf("%0u\t %0u\n", x_data[i], y_data[i]);
    }
    printf("end of init\n");
    
  fprintf(fp, "plot '-'\n");
  for (i=0; i<=100; i++)
  {
    printf ("%0u\t %0u\n", x_data[i], y_data[i]); // to check x, y
    
    fprintf(fp,"set xrange[0:100]\n"); //fprintf(fp, "set xrange[0:%0u]\n", i);
    fprintf(fp, "set yrange[0:255]\n");
    fprintf(fp, "plot %0u, %0u", x_data[i], y_data[i]);
    fprintf(fp, " with points\n");
    fflush(fp);
   }
    
    pclose(fp);
    
    
  return 0;
  }

I tried to keep two variables, because when tested on textfile - gnuplot automatically takes 1st column as 'x' and second as 'y', but not in my code - it uses both variables as 'y' (two flat lines). I tried to force gnuplot to use two variables in last fprintf command:

fprint(fp, "plot %0u, %0u"), x_data[i], y_data[i]);
fprint(fp, "**using 1:2** with points\n");

but gnuplot sends back 'unexpected or unrecognized token ' I also try to use '-' method but gnuplot sends 'Bad data on line 1 of file', even when I forced special characters ''' Another try was to use fflush(fd); outside of 'for (i)' loop, same result (flat graph).

This c++ is in Debian system on Raspberry pi 4. I use Geany for coding and there is no difference when I call 'gnuplot-x11', or simple 'gnuplot'. I know there is a way to save data to file, even temporary - it works, but I want to check all possible solutions before I retreat. Thanks for your support.

EDIT

thanks to suggested idea I changed the code. Data is sent directly after 'plot'. new part looks as follows:

  fprintf(fp, "plot '-' using 2:1 \t");
   for (i=0; i<=1000; i++)
   {
    fprintf(fp, "%0u, %0u\t", x_data[i], y_data[i]);
    fprintf(fp, "with points\n");
   }

And it works as I wanted.

Upvotes: 1

Views: 76

Answers (1)

sTadek
sTadek

Reputation: 11

after improvements working code looks like:

#include <stdio.h>
//#include <fcntl.h>
//#include <unistd.h>
//#include <sys/ioctl.h>

int main (int argc, char **argv)
  {
  
  FILE *fp = NULL;
  fp =popen ("gnuplot-x11 -persist", "w");
  
  int i =0;
  unsigned int x_data[100000],
               y_data[100000];
  
  for (i=0; i<=100; i++)
    {
    x_data[i]=i;
    y_data[i]=2*i;
  
    //printf("%0u\t %0u\n", x_data[i], y_data[i]); //to check data
    }
    printf("end of init\n");
    
  fprintf(fp, "set xrange[0:%0u]\n", i); 
  fprintf(fp, "set yrange[0:255]\n");  

  fprintf(fp, "plot '-' using 2:1 \t");
  
  for (i=0; i<=100; i++)
  {
    fprintf(fp, " %0u, %0u\t", x_data[i], y_data[i]);
    fprintf(fp, "with points\n");
  }
    
    fflush(fp);
    pclose(fp);
    
  return 0;
  }

The solution was to improve inline 'plot' syntax which starts before 'for' loop and finishes inside the loop. Now it has only one 'plot' command for data and this is very last gnuplot command - after any 'set' instructions.

Upvotes: 0

Related Questions