Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaPlot and gnuplot

I'm desperately trying to get Java and gnuplot to play nice. I've started using JavaPlot and have added the jar to the classpath (using Eclipse).

I've also downloaded gnuplot and have placed it in a safe place.

First question, all examples given by JavaPlot are assuming you have put gnuplot in the right place, where this is I have no idea. Therefore their example of:

import com.panayotis.gnuplot.JavaPlot;

public class test {
    public static void main(String[] args) {
        JavaPlot p = new JavaPlot();
        p.addPlot("sin(x)");
        p.plot();
    }
}

Will only work if gnuplot is added to the classpath, any ideas on where that might be and how?

Not to worry though, as you can define the location of gnuplot in the constructor of JavaPlot, like so:

import com.panayotis.gnuplot.JavaPlot;

public class test {
    public static void main(String[] args) {
        JavaPlot p = new JavaPlot("D:/Eclipse/gnuplot/binary/pgnuplot.exe");
        p.addPlot("sin(x)");
        p.plot();
    }
}

This does something, if you're quick you can see a graph appear (correctly, can see the sine wave) and then immediately disappear. I've read online that in the actual gnuplot application this is common when using Windows and that a '-persist' must be added after the plot. Fortunately JavaPlot also has a function that does that:

p.setPersist(true);

But in my case it does nothing. So second question, anyone used gnuplot, JavaPlot, and Windows 7 64bit before and know how to do this? From my Googling I understand pgnuplot is the correct .exe to run?

What am I missing? What am I doing wrong?

like image 594
ritchie888 Avatar asked Mar 06 '26 08:03

ritchie888


1 Answers

I think I may have a workaround for you, as I ran into the same sort of thing today when accessing JavaPlot on Windows 7 (32 bit here though). Yes, pgnuplot.exe is the one you want, however you do not need to explicitly setPersist if you do not want to because JavaPlot does that for you. What I had to do was go through the source code and comment out a line.

In GnuPlotParameters, I see the code

/* Finish! */
bf.append("quit").append(NL);

This is lines 198-199. Then the plot windows stays open. Now, what this also does is leave open gnuplot. If you do not mind, you can see your graphs this way. Have not figured out yet how to close gnuplot while leaving the plot window open.

EDIT:

Maybe a more appropriate way is not to comment out line 199 and go with this:

bf.append("pause -1").append(NL);

/* Finish! */
bf.append("quit").append(NL);

In this manner, the pause dialog comes up. This allows you to see the plot. When you dismiss the dialog, everything goes bye-bye.

like image 169
demongolem Avatar answered Mar 07 '26 22:03

demongolem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!