tags:

views:

1358

answers:

7
+2  Q: 

C++ and gnuplot

Hi everybody! This is my first post and I'm quite a novice on C++ and compiling in general.

I'm compiling a program which requires some graphs to be drawn. The program create a .dat file and then i should open gnuplot and write plot '.dat'. That's fine.

Is there a way to make gnuplot automatically open and show me the plot I need? I should use some system() function in the code to call gnuplot but how can I make him plot what I need?

Sorry for my non-perfect English :s

Thanks for the attention anyway!

+1  A: 

Yes you can. You can make a file that has the commands you would otherwise type in to set up the plot and open gnuplot running from that file. This link has an article that explains how to do it. You can also output to an EPS or other graphical output formats and display the plot using another widget that reads in the file.

ConcernedOfTunbridgeWells
A: 

Sometimes it is as easy as one may think

    gnuplot file

where file is neither your data nor your result file, but a file with the command you would type in the commandline. Just enter there your commands you need (either constant file you have or generate it). After executing all commands in that file gnuplot exits.

flolo
this is not working! i really don't know how to do :D
Marco Orrù
A: 

You might need to set a terminal type. Read the gnuplot documentation about that. Also, please modify your original question, don't put additional questions into the answers; this isn't a forum thread.

Svante
Oh ok i'm sorry, how should i modify the title?
Marco Orrù
A: 

You may need to use the '-persist' flag to the command. I know that on *nix systems, this flag is required if you want the plot window to remain after the gnuplot process has completed and exited.

gnuplot -persist commands.gp

Also, you can put as many gnuplot commands as you like in the file. The file acts sort of like a batch script in this regard.

A: 

you might need to add a line

pause -1

This will show the plot until return has been pressed What you are probably seeing is that gnuplot runs and exits before the plot has time to be displayed.

KeithB
Great thanx..it works now!The only problem left is that it shows a window titled Gnuplot Pause which waits for the user to click ok..the problem is that it is right over the plot :D but that's ok i'm satisfied :D thanx again
Marco Orrù
+2  A: 

Depending on your OS, you might be able to use popen(). This would let you spawn a gnuplot process and just just write to it like any other FILE*.

If you have datapoints to plot, you can pass them inline with the plot "-" ... option. Similarly, you may want to explore set data style points/lines/linespoints/etc options.


Without pause or persist, gnuplot will terminate upon end-of-input-stream. In your example case, that would be when the end of the file is reached.


To produce (write) an output file (graph), use:

set terminal png small
set output "filename.png"

There's lots of options to set terminal. Png is usually there. If not, perhaps gif, tiff, or jpeg?

Watch out for overwriting the file!

You may want to use set size 2,2 to make a larger graph. Some set terminal variants also allow you to specify the size.

Mr.Ree
Thanx that's the perfect solution i was looking for!
Marco Orrù
A: 

I'm learning this today too. Here is a small example I cooked up.

#include <iostream>
#include <fstream> 
using namespace std;
int main(int argc, char **argv) {
    ofstream file("data.dat");
    file << "#x y" << endl;
    for(int i=0; i<10; i++){
        file << i << ' ' << i*i << endl;
    }
    file.close();
    return 0;
}

Save that as plot.cpp and compile that with g++:

g++ plot.cpp -o plot

Run the program to create the .dat file:

./plot

Save the following gnuplot script as plot.plt:

set terminal svg enhanced size 1000 1000 fname "Times" fsize 36
set output "plot.svg"
set title "A simple plot of x^2 vs. x"
set xlabel "x"
set ylabel "y"
plot "./data.dat" using 1:2 title ""

Run the script with gnuplot to generate your .svg file:

gnuplot plot.plt

The resulting plot will be in plot.svg. If you leave out the first couple lines that specify the output, it will render in a window. Have fun!

Drew Wagner