gnuplot_test


gnuplot_test, a C++ code which illustrates how commands and data for the gnuplot() graphics program can be generated, to illustrate the results of a calculation.

Ordinarily, gnuplot() is used interactively; that is, the user invokes the program, and then describes the plot to be created. The data for the plot is created either by a formula, or by reference to some data file created earlier.

It is often extremely important to be able to create graphical images of the results of an executable program. If the user is familiar with gnuplot(), then the program can be modified to create a suitable data file, and, once the program has completed, the user can run gnuplot() and use that data file to create a graph.

However, if the program is going to create the data file, it can just as easily create a file containing the appropriate commands to gnuplot() that will create the desired image. In this way, the user can avoid having to recall the correct commands in the correct order to create the plot.

Thus, the program might create two files, called, perhaps data.txt and commands.txt. The command file would contain gnuplot() commands, including one that refers to the data file itself. To create the graphics image, the user would simply type

        gnuplot < commands.txt
      

This directory includes some sample programs which carry out numerical calculations, creating data and command files that are used in exactly this way.

Some common plot types include:

Licensing:

The information on this web page is distributed under the MIT license.

Languages:

gnuplot_test is available in a non-language specific version and a C version and a C++ version and a Fortran77 version and a Fortran90 version and a MATLAB version and an Octave version and a Python version.

Related Data and Programs:

analemma, a C++ code which evaluates the equation of time, a formula for the difference between the uniform 24 hour day and the actual position of the sun, creating data that can be plotted by gnuplot, based on a program by Brian Tung.

brownian_motion_simulation, a C++ code which simulates Brownian motion in an M-dimensional region, creating graphics files that can be displayed with gnuplot().

disk_grid, a C++ code which computes grid points that lie inside a disk of user specified radius and center in 2D, using gnuplot() to create an image of the grid.

dislin, a C++ code which uses dislin(), a scientific graphics package, by Helmut Michels.

edge, a C++ code which defines some test functions in 1D, 2D and 3D for the detection of edges or discontinuities. It uses gnuplot() to display the jumps in curves and surfaces.

fair_dice_simulation, a C++ code which simulates N games in which two fair dice are thrown and summed, creating graphics files for processing by gnuplot.

fd1d_advection_ftcs, a C++ code which applies the finite difference method (FDM) to solve the time-dependent advection equation ut = - c * ux in one spatial dimension, with a constant velocity, using the forward time, centered space (FTCS) difference method, writing graphics files for processing by gnuplot.

fd1d_advection_lax, a C++ code which applies the finite difference method (FDM) to solve the time-dependent advection equation ut = - c * ux in one spatial dimension, with a constant velocity, using the Lax method to treat the time derivative, writing graphics files for processing by gnuplot.

fd1d_advection_lax_wendroff, a C++ code which applies the finite difference method (FDM) to solve the time-dependent advection equation ut = - c * ux in one spatial dimension, with a constant velocity, using the Lax-Wendroff method to treat the time derivative, writing graphics files for processing by gnuplot.

gnuplot_i_examples, C++ codes which demonstrate the use of the GNUPLOT_I library for interactive runtime gnuplot graphics.

high_card_simulation, a C++ code which simulates a situation in which you see the cards in a deck one by one, and must select the one you think is the highest and stop; the program uses GNUPLOT for graphics.

ising_2d_simulation, a C++ code which carries out a Monte Carlo simulation of an Ising model, a 2D array of positive and negative charges, each of which is likely to flip to be in agreement with neighbors, using gnuplot to make images of the initial and final configurations.

lebesgue, a C++ code which is given a set of nodes in 1D, and plots the Lebesgue function, and estimates the Lebesgue constant, which measures the maximum magnitude of the potential error of Lagrange polynomial interpolation, and which uses gnuplot to make plots of the Lebesgue function.

lobatto_polynomial, a C++ code which evaluates Lobatto polynomials, similar to Legendre polynomials except that they are zero at both endpoints.

lorenz_ode, a C++ code which approximates solutions to the Lorenz system, creating output files that can be displayed by Gnuplot.

naca, a C++ code which can take the parameters of certain NACA airfoils and return the coordinates of a sequence of points that outline the wing shape. The data can be displayed with gnuplot.

nearest_interp_1d, a C++ code which interpolates a set of data using a piecewise constant interpolant defined by the nearest neighbor criterion, creating graphics files for processing by gnuplot.

ornstein_uhlenbeck, a C++ code which approximates solutions of the Ornstein-Uhlenbeck stochastic ordinary differential equation (SODE) using the Euler method and the Euler-Maruyama method, and creating graphics files for processing by gnuplot.

poisson_simulation, a C++ code which simulates a Poisson process in which events randomly occur with an average waiting time of Lambda, creating output for graphics by gnuplot.

pwl_interp_1d, a C++ code which interpolates a set of data using a piecewise linear function, creating graphics files for processing by gnuplot.

sde, a C++ code which illustrates properties of stochastic ordinary differential equations (SODE), and common algorithms for their analysis, including the Euler method, the Euler-Maruyama method, and the Milstein method, making graphics files for processing and display by gnuplot, by Desmond Higham;

spiral_data, a C++ code which computes a velocity vector field that satisfies the continuity equation, writing the data to a file that can be plotted by gnuplot.

spring_ode2, a C++ code which shows how gnuplot() can illustrate a solution of the ordinary differential equation (ODE) that describes the motion of a weight attached to a spring.

stochastic_diffusion, a C++ code which implements several versions of a stochastic diffusivity coefficient, and displays sample results using gnuplot.

stochastic_heat2d, a C++ code which implements a finite difference method (FDM) for the steady (time independent) 2D heat equation, with a stochastic heat diffusivity coefficient, using gnuplot to illustrate the results.

string_simulation, a C++ code which simulates the behavior of a vibrating string, creating files that can be displayed by gnuplot.

svd_snowfall, a C++ code which reads a file containing historical snowfall data and analyzes the data with the Singular Value Decomposition (SVD), and plots created by gnuplot().

vandermonde_interp_1d, a C++ code which finds a polynomial interpolant to data y(x) of a 1D argument, by setting up and solving a linear system for the polynomial coefficients, involving the Vandermonde matrix, creating graphics files for processing by gnuplot().

Reference:

  1. Philipp Janert,
    Gnuplot in Action: Understanding Data with Graphs,
    Manning, 2010,
    ISBN13: 978-1-933988-39-8,
    LC: QA276.4.J37.
  2. Thomas Williams, Colin Kelley,
    gnuplot 4.4, An Interactive Plotting Program,
    The Free Software Foundation, 2010.
  3. The GNUPLOT Website.

Examples and Tests:

album_bar lists the year, and total number of music albums (LP's, cassettes, CD's and downloads) sold each year from 2007 to 2017. This data is plotted as a bar graph.

automobile_scatter contains prices and weights of cars available in 1985. A scatter plot is to be made.

brownian_2d_plot plots data representing 1000 steps of Brownian motion in two dimensions. Unlike a typical y=f(x) plot, this data wanders around the page.

bulgaria_plot takes data about the Bulgarian population over time and makes a plot of (time,population).

caffeine_scatter seeks a relationship between the percentage of blindness due to cataracts, and the daily intake of caffeine, in a number of countries. A scatter plot is produced.

corkscrew_plot3d generates (X,Y,Z) points that lie along a 3D curve looking something like a corkscrew.

correlation_heat_map takes an 8x8 correlation matrix and draws an 8x8 grid of squares, colored white for the highest values, yellow, then red, and black for the lowest.

corvette_scatter considers the resale price for Corvettes by model year, displaying the results as a scatter plot.

damped_sine evaluates the damped sine correlation function for a range of values of its argument. A plot of the function is desired.

drug_dosage_plots depicts measurements over 48 hours of the blood level concentration of a medicinal drug. The drug needs to reach a certain level to have an effect, but must not exceed the toxic level. A graphic is created which shows, on one plot, the concentration over time, the minimal effective level, and the maximum nonlethal leval.

faithful_histogram contains the waiting time in minutes between successive eruptions of the Old Faithful geyser.

geyser_scatter looks for relations between the duration in minutes of the eruption and following waiting times for the Old Faithful geyser.

grid_surface records, on a 41x41 grid over [-2,2]x[-2,+2], the values z = exp(-(x^2+y^2)) * cos(0.25*x) * sin(y) * cos(2*(x^2+y^2)). The data is plotted as a surface.

humps_plot plots the function y=humps(x) over [0,2]. Instead of a file of data, gnuplot() works directly with a formula for the function.

insect_scatter3d involves 3 data sets, each containing 10 measurements of 3 quantities for each of 3 species of insect. The quantities are first tarsus width, second tarsus width, and maximum width of the aedeagus. It is of interest to know whether these three measurements are enough to differentiate between members of the three species. A 3D scatter plot is created.

least_squares_plots compares 15 pairs of (x,y) data, the least squares line that approximates their behavior, and a quadratic curve that is much closer to the data.

mario_fill makes a simple image of Mario, by constructing a grid of squares filled with color.

mark_points adds green and red dots to mark the first and last data points in a plot.

nile_histogram makes a histogram of the yearly measurement of the height of the Nile at maximum flood. By lumping the data into bins, it is easier to see the range of flood heights, and the probability of various values in the range.

orbital_fill_contour makes a color contour plot of orbital data.

predator_plot3d models the populations of predators (foxes) and prey (rabbits) over time. A 3D plot of (time, rabbits, foxes) is created.

president_heights_bar plots the heights of US presidents in inches, as a bar plot.

price_plots reads a table of average monthly prices for 11 consumer products, between February 2008 and February 2018, as compiled by the Bureau of Labor Statistics, and plots together the prices of three of the items.

snowfall_plot makes a line plot of the snowfall at Michigan Tech from 1890 to 2017.

string_simulation simulates the behavior of a string that obeys the wave equation. The shape of the string is to be plotted over successive times.

temperature_scatter3d shows temperatures and locations in the US where the January temperature was recorded.

track_bar considers an eye-tracking experiment in which the eye was focused on different regions for different durations. Each region has a text label. A bar graph is desired, in which the bar for each duration is given the appropriate label.

volcano_fill creates a color-filled contour plot of a volcano's height, using an MxN table of data.

vortex_subplots shows pressure and velocity vector subplots in a single figure.


Last revised on 29 September 2026.