3

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?

david
  • 3,225
  • 9
  • 30
  • 43
ritchie888
  • 583
  • 3
  • 12
  • 27

5 Answers5

1

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.

david
  • 3,225
  • 9
  • 30
  • 43
demongolem
  • 9,474
  • 36
  • 90
  • 105
0

I use eclipse for debugging and happen to be using this package. I figured out how to fix this. Add the following to your code. The setPersist(true) doesn't seem to work for some reason.

p.set("term", "x11 persist");
david
  • 3,225
  • 9
  • 30
  • 43
0

try this

try {
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("D:/Projet/X-Gnuplot_4.6.0_rev6/Bin/gnuplot/bin/gnuplot.exe");

    java.io.OutputStream opStream = proc.getOutputStream();
    PrintWriter gp = new PrintWriter(new BufferedWriter(new OutputStreamWriter(opStream)));
    gp.println("plot sin(x); pause mouse close;\n");  
    gp.close();

    int exitVal = proc.waitFor();
    System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
    System.out.println(e.toString());
    e.printStackTrace();
}

it works for me

david
  • 3,225
  • 9
  • 30
  • 43
0

Try JavaGnuplotHybrid: https://github.com/mleoking/JavaGnuplotHybrid

It solves the problem of immediately disappear.

Here is the example for a 2D plot:

public void plot2d() {
    JGnuplot jg = new JGnuplot();
    Plot plot = new Plot("") {
        {
            xlabel = "x";
            ylabel = "y";
        }
    };
    double[] x = { 1, 2, 3, 4, 5 }, y1 = { 2, 4, 6, 8, 10 }, y2 = { 3, 6, 9, 12, 15 };
    DataTableSet dts = plot.addNewDataTableSet("2D Plot");
    dts.addNewDataTable("y=2x", x, y1);
    dts.addNewDataTable("y=3x", x, y2);
    jg.execute(plot, jg.plot2d);
}

enter image description here

Changwang Zhang
  • 2,467
  • 7
  • 38
  • 64
-1

replace your

p.addPlot("sin(x)");

by

p.addPlot("sin(x); pause 100;");

it only appears for 100 seconds fsd