java - 使用 JavaPlot/Gnuplot 进行 3D 绘图

标签 java eclipse gnuplot javaplot

我正在尝试做标题所说的事情。显然 gnuplot 能够做到这一点,但我想使用 JavaPlot 来调用它。 Graph3D class in JavaPlot 让我认为这是可能的,但由于我没有找到 3D 示例,并且几乎没有关于 JavaPlot 的文档,所以我只知道如何进行此操作。如果有人已经知道如何做到这一点,并且我的努力是为了重新发明轮子,请启发我,但目前我将继续进行,就好像以前没有人尝试过这样做一样。

查看GNUPlot类,有一个plot方法,但是splot方法被注释掉了,GNUPlotExec类中没有对应的方法。我尝试添加一个,但目前它仍然以 2D 形式绘制。为了充分披露,我没有从头开始制作这个,而是修改了当前的绘图方法。

这是被注释掉的 GNUPlot.class splot 方法

    public void splot() throws GNUPlotException {
    exec.splot(param, term);
}

这是从plot方法派生的GNUPlotExec.class splot方法

void splot(GNUPlotParameters par, GNUPlotTerminal terminal) throws GNUPlotException {
    try {
        final GNUPlotTerminal term = terminal;  // Use this thread-aware variable instead of "terminal"
        final String comms = getCommands(par, term); // Get the commands to send to gnuplot
        final Messages msg = new Messages();    // Where to store messages from output threads

        /* Display plot commands to send to gnuplot */
        GNUPlot.getDebugger().msg("** Start of splot commands **", Debug.INFO);
        GNUPlot.getDebugger().msg(comms, Debug.INFO);
        GNUPlot.getDebugger().msg("** End of splot commands **", Debug.INFO);

        /* It's time now to start the actual gnuplot application */
        String[] command;
        if (ispersist) {
            command = persistcommand;
        } else {
            command = nopersist;
        }
        command[0] = getGNUPlotPath();
        {
            String cmdStr = "";
            for (String cmd : command) {
                cmdStr += cmd + " ";
            }
            GNUPlot.getDebugger().msg("exec(" + cmdStr + ")", Debug.INFO);
        }
        final Process proc = Runtime.getRuntime().exec(command);

        /* Windows buffers DEMAND asynchronus read & write */

        /* Thread to process the STDERR of gnuplot */
        Thread err_thread = new Thread() {

            public void run() {
                BufferedReader err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
                StringBuffer buf = new StringBuffer();
                String line;
                try {
                    while ((line = err.readLine()) != null) {
                        line = parseErrorLine(line, "gnuplot> splot");
                        line = line.replace("input data ('e' ends) >", "").trim();   // Remove entries having the "input data" prompt
                        if (line.equals("^")) {
                            line = "";
                        }  // Ignore line with error pointer
                        if (!line.equals("")) {     // Only take care of not empty lines
                            if (line.indexOf(GNUPlotParameters.ERRORTAG) >= 0) {
                                msg.error = "Error while parsing \'splot\' arguments.";    // Error was found in plot command
                                break;
                            }
                            buf.append(line).append('\n');
                        }
                    }
                    err.close();
                    msg.output = buf.toString(); // Store output stream
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        };
        /* Thread to process the STDOUT of gnuplot */
        err_thread.start();
        Thread out_thread = new Thread() {

            public void run() {
                msg.process = term.processOutput(proc.getInputStream());    // Execute terminal specific output parsing
            }
        };
        out_thread.start();

        /* We utilize the current thread for gnuplot execution */
        OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());
        out.write(comms);
        out.flush();
        out.close();


        try {
            proc.waitFor(); // wait for process to finish
            out_thread.join();  // wait for output (terminal related) thread to finish
            err_thread.join();  // wait for error (messages) output to finish
        } catch (InterruptedException ex) {
            throw new GNUPlotException("Interrupted execution of gnuplot");
        }

        /* Find the error message, if any, with precendence to the error thread */
        String message = null;
        if (msg.error != null) {
            message = msg.error;
        } else {
            message = msg.process;
        }

        /* Determine if error stream should be dumbed or not */
        int level = Debug.VERBOSE;
        if (message != null) {
            level = Debug.ERROR;
        }
        GNUPlot.getDebugger().msg("** Start of error stream **", level);
        GNUPlot.getDebugger().msg(msg.output, level);
        GNUPlot.getDebugger().msg("** End of error stream **", level);

        /* Throw an exception if an error occured */
        if (message != null) {
            throw new GNUPlotException(message);
        }

    } catch (IOException ex) {
        throw new GNUPlotException("IOException while executing \"" + getGNUPlotPath() + "\":" + ex.getLocalizedMessage());
    }

}

这是我正在尝试运行的测试

public static void main(String[] args) {

    GNUPlot p = new GNUPlot("path goes here");

    FunctionPlot myPlot = new FunctionPlot("tan(x)");

    p.addPlot(myPlot);

    p.splot();
}

我相信 gnuplot 执行的命令是

gnuplot> _gnuplot_error = 1
gnuplot> plot tan(x) title 'tan(x)' ; _gnuplot_error = 0
gnuplot> if (_gnuplot_error == 1) print '_ERROR_'
gnuplot>          undefined function: if

当然应该说splot,而不是plot

最佳答案

想通了。我需要添加

p.new3DGraph();

在 main 之前 p.addPlot(myPlot); 希望这能帮助其他人,因为 JavaPlot 上什么都没有

关于java - 使用 JavaPlot/Gnuplot 进行 3D 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11365693/

相关文章:

java - 构建多个 servlet 请求 url 模式

java - Selenium webdriver 使用相对路径上传下载文件

java - Android 应用程序的典型 .gitignore 文件

Gnuplot:特定位置的垂直线

java - 为什么我无法从组件绑定(bind)中获取提交的值?

java - Java spring检测页面变化

eclipse - Tomcat 在 Eclipse 中启动但无法连接到 http ://localhost:8085/

来自数据文件的 Gnuplot 3d 时间动画

c# - 使用 C# 在 GNU PLOT 中绘制图形

java - 从 cron 作业调用 REST Web 服务