How to Plot Data in C

Jinku Hu Oct 12, 2023
  1. Use gnuplot Function to Check End-Of-File Indicator on File Stream
  2. Use popen Function to Stream Plot Data to gnuplot Process
How to Plot Data in C

This article will explain several methods of how to plot data in C.

Use gnuplot Function to Check End-Of-File Indicator on File Stream

gnuplot is a powerful plotting program that can be utilized to display plots and save them as files. It is generally used with file data sets with the simple column-based format where each column is delimited with a single space, as shown below. In this example, we use the popen function to stream the commands to the gnuplot program and plot the data that is store in a separate file. The gnuplot documentation can be read on this page. In this case, we are going to use only the simplest commands to demonstrate the usage.

The plot command is the core part that takes different arguments and parameters to process and render the plot. We are supplying a file with 2 columns of numbers that need to be plotted as a line chart. The command has the following format plot 'input.txt' t 'Price' w lp, which is constructed using fprintf and written to the file stream pointer returned from the popen call. t specifier in the command is shorthand of title and w - with keyword that indicates the style of the chart. In this case, lp (line points) style is chosen to represent multiple price points over a specific period. Notice that we explicitly fflush the stream to ensure the data is delivered to the gnuplot program and finally pause the program with the getchar function to make sure the plot is displayed until the user closes it.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  const char *filename = "input.txt";

  FILE *gnuplot = popen("gnuplot", "w");
  if (!gnuplot) {
    perror("popen");
    exit(EXIT_FAILURE);
  }

  fprintf(gnuplot, "plot \"%s\" t 'Price' w lp\n", filename);
  fflush(gnuplot);
  fprintf(stdout, "Click Ctrl+d to quit...\n");
  getchar();

  pclose(gnuplot);
  exit(EXIT_SUCCESS);
}

Input File Format:

2005 49
2006 52
...
2019 154
2020 127
2021 147

Use popen Function to Stream Plot Data to gnuplot Process

Alternatively, we can stream the previously stored data in a separate file directly from the program memory. The popen function is used again to open a pipe communication to the gnuplot program and then send the numbers stored in arrays with special formatting. In this example, the command has the format - plot '-' u 1:2 t 'Price' w lp, followed by the data elements and finally terminated with e character. Each integer should be separated with space and passed with a similar format as the previous example’s input file. Thus, we utilize the fprintf function to write formatted text to the gnuplot pipe stream. Note that, file streams opened with the popen call should be closed with the pclose function.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  int x[] = {2015, 2016, 2017, 2018, 2019, 2020};
  int y[] = {344, 543, 433, 232, 212, 343};

  FILE *gnuplot = popen("gnuplot", "w");
  if (!gnuplot) {
    perror("popen");
    exit(EXIT_FAILURE);
  }

  fprintf(gnuplot, "plot '-' u 1:2 t 'Price' w lp\n");
  for (int i = 0; i < 6; ++i) {
    fprintf(gnuplot, "%d %d\n", x[i], y[i]);
  }
  fprintf(gnuplot, "e\n");
  fprintf(stdout, "Click Ctrl+d to quit...\n");
  fflush(gnuplot);
  getchar();

  pclose(gnuplot);
  exit(EXIT_SUCCESS);
}
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook