Graphics with Gnuplot from C and Python language

If you are doing a lot of calculations/simulations, like me, using C or/and Python language, you also need to visualize your simulated data. One way to do it is just to generate/write a “data” file and plot it with your plotting preferred package after the simulation is finished. But maybe, sometimes, you want to see the graph in real time, so, here I would like to show a small C and Python program which calls Gnuplot and save the plot in png format.

C Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
/*
	Author: Ioan Vancea (www.vioan.ro)
	*****************************/
int main()
{
	FILE *pipe = popen("gnuplot -persist","w");
	fprintf(pipe, "set samples 40\n");
	fprintf(pipe, "set isosamples 40\n");
	fprintf(pipe, "set hidden3d\n");
	fprintf(pipe, "set xrange [-8.000:8.000]\n");
	fprintf(pipe, "set yrange [-8.000:8.000]\n");
	fprintf(pipe, "set zrange [-2.000:2.000]\n");
	fprintf(pipe, "set terminal png\n");
	fprintf(pipe, "set output 'graph.png'\n");
	fprintf(pipe, "set title 'We are plotting from C'\n");
	fprintf(pipe, "set xlabel 'Label X'\n");
	fprintf(pipe, "set ylabel 'Label Y'\n");
	/* fprintf(pipe, "plot 'datafile.dat' using 1:2\n");*/
	fprintf(pipe, "splot cos(x)+cos(y)\n");
 
  close(pipe);
  return 0;
}

The output:

Python Example (I use Gnuplot.py):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env python
 
'''
	Author: Ioan Vancea (www.vioan.ro)
'''
 
import math
import Gnuplot
gp = Gnuplot.Gnuplot(persist=1)
x = range(1000)
y = [math.sin(a) + math.cos(a) + math.tan(a) for a in x]
data = Gnuplot.Data(x, y, title='Plotting from Python')
gp('set data style lines')
gp('set xlabel "Label x"')
gp('set ylabel "Label y"')
gp.plot(data);
gp.hardcopy(filename="pyplot.png",terminal="png")

The output:

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

[...] my previous post I discussed how to call Gnuplot from C and Python language. If you are using Perl, then, here is [...]

You may want to check out this page, it has some information about calling gnuplot from C, C++, Python and others:
http://ndevilla.free.fr/gnuplot/
Hope it helps

Thanks Nicolas, nice, I didn’t know about “gnuplot_i”.

I like the Pylab/Matplotlib package:
http://matplotlib.sourceforge.net/
If you are familiar with plotting in Matlab, it will be easy to pick up. I have found it to be very good for publication-quality 2D graphics (it doesn’t do 3D).

Hi,

I would be interested in bidirectional piping of gnuplot. I want to use gnuplot for both plotting and fitting in my code, and I want to read gnuplot’s output. Is it possible to do?
Thanks,
Dan

I didn’t try that, but I think you can also use some intermediate/temporary files for that.

Hey i wanted to know if there is a way of plotting graphs from my c++ program without saving the data on to a file.. I am generating random numbers and calculating their averages… I need the averages to be plotted on the graph but i dont want to save the averages to a file.

Thanks

I tried ypur code in an Xcode project I’m working on.

I get the error:

sh: gnuplot: command not found

Can anyone help me!

Thanks

It means that you don’t have gnuplot installed or maybe is not in your PATH. Try to install it using macports or fink. Check their website and the documentation to see how to use them.

Thanks, just what I was looking for. I found gnuplot_i first but this is way simpler :)
By the way if you want to plot data from your c-program, first tell gnuplot to read it from stdin:
fprintf(pipe, “plot ‘-’\n”);
Then print all the data you want (probably in a loop)
fprintf(pipe, “%lf %lf\n”, variable1, variable2);

When you’re done tell gnuplot that data ends here
fprintf(pipe, “e\n”);

Leave a comment

(required)

(required)