如何在JavaPlot中输出png文件

时间:2012-03-21 14:13:02

标签: java png gnuplot

我使用“> gnujavaplot,我没有找到制作输出图像文件的方法。我只是在gnuplot窗口中绘图,我需要在png文件中输出绘图。

我需要这样的东西: $ a set terminal png $ set output“filename.png” 在gnujavaplot。

感谢。

3 个答案:

答案 0 :(得分:6)

我只是复制我的实际代码,没有编辑,所以有一些开销,但我认为你应该得到的东西。有趣的部分从

开始
ImageTerminal png = new ImageTerminal();

并且不要错过代码的结尾!

问候

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.panayotis.gnuplot.JavaPlot;
import com.panayotis.gnuplot.plot.DataSetPlot;
import com.panayotis.gnuplot.style.NamedPlotColor;
import com.panayotis.gnuplot.style.PlotStyle;
import com.panayotis.gnuplot.style.Style;
import com.panayotis.gnuplot.terminal.ImageTerminal;

public class main {
public static void main(String[] args) {
    double[][] values = new double[3][2];
    values[0][0] = 0.1;
    values[0][1] = 0.3;
    values[1][0] = 0.4;
    values[1][1] = 0.3;
    values[2][0] = 0.5;
    values[2][1] = 0.5;

    double[][] values2 = new double[3][2];
    values2[0][0] = 0.2;
    values2[0][1] = 0.0;
    values2[1][0] = 0.7;
    values2[1][1] = 0.1;
    values2[2][0] = 0.6;
    values2[2][1] = 0.5;

    PlotStyle styleDeleted = new PlotStyle();
    styleDeleted.setStyle(Style.POINTS);
    styleDeleted.setLineType(NamedPlotColor.GRAY80);

    PlotStyle styleExist = new PlotStyle();
    styleExist.setStyle(Style.POINTS);
    styleExist.setLineType(NamedPlotColor.BLACK);

    DataSetPlot setDeleted = new DataSetPlot(values);
    setDeleted.setPlotStyle(styleDeleted);
    setDeleted.setTitle("deleted EMs");

    DataSetPlot setExist = new DataSetPlot(values2);
    setExist.setPlotStyle(styleExist);
    setExist.setTitle("remaining EMs");

    ImageTerminal png = new ImageTerminal();
    File file = new File("/home/testuser/plot.png");
    try {
        file.createNewFile();
        png.processOutput(new FileInputStream(file));
    } catch (FileNotFoundException ex) {
        System.err.print(ex);
    } catch (IOException ex) {
        System.err.print(ex);
    }

    JavaPlot p = new JavaPlot();
    p.setTerminal(png);

    p.getAxis("x").setLabel("yield");
    p.getAxis("y").setLabel("biomass");
    p.getAxis("x").setBoundaries(0.0, 1.0);
    p.getAxis("y").setBoundaries(0.0, 1.0);
    p.addPlot(setDeleted);
    p.addPlot(setExist);
    p.setTitle("remaining EMs");
    p.plot();

    try {
        ImageIO.write(png.getImage(), "png", file);
    } catch (IOException ex) {
        System.err.print(ex);
    }
}

}

答案 1 :(得分:1)

值得注意的是,除了christo提供的代码之外,当你切换javaplot的终端时,它会阻塞并崩溃线程,因为gnuplot正在等待用户关闭它,因为它设置了到png文件对象,没有办法做。修复此问题的方法(至少在Windows 7中)是将persist属性设置为false,如下所示:

p.setPersist(false);

这将确保一切顺利,并且没有问题。

答案 2 :(得分:0)

我也在寻找该问题的答案,但找不到。 但是,在看到有关如何导出到“ .eps”文件的示例后,我将其调整为PNG。 以下是x在[0,10]

中时抛物线的简单图形示例
//PNG export
import java.io.File;
import com.panayotis.gnuplot.terminal.FileTerminal;
import com.panayotis.gnuplot.terminal.GNUPlotTerminal;
//JavaPlot Styling
import com.panayotis.gnuplot.JavaPlot;
import com.panayotis.gnuplot.plot.AbstractPlot;
import com.panayotis.gnuplot.style.PlotStyle;
import com.panayotis.gnuplot.style.Style;



public class HOPE {

    public static void main(String[] args) {
        String className="parabola";
        String title="teste";
        //Generates a file in .png
        File file = new File("IMG/statistics_"+ title + "_" + className + ".png");
    //Creates a plot
        JavaPlot plot = new JavaPlot();
    //Creates a terminal class that interacts with Gnuplot without showing the graph
        GNUPlotTerminal terminal = new FileTerminal("png","IMG/statistics_" + title + "_" + className + ".png");
    plot.setTerminal(terminal);
        //Configurations of axis labels
    plot.set("xlabel", "\"LEI-ISEP\"");
    plot.set("ylabel", "\"" + title + "\"");
    plot.set("autoscale", "ymax");
        //Generate the parabola data
    int[][] data = new int[10][2];
    for (int i = 0; i < 10; i++) {
        data[i][0] = i;
        data[i][1] = i*i; //Could hava used Math.pow(i,2)
    }
        //Appends the parabola to the graphic
    plot.addPlot(data);
        //Defines the style of the graph
    PlotStyle stl = ((AbstractPlot) plot.getPlots().get(0)).getPlotStyle();
    stl.setStyle(Style.LINESPOINTS);
        //Hides the label on the line
        plot.setKey(JavaPlot.Key.OFF);
        //Executes the plot (without showing) and saves the image in IMG directory in your project directory
    plot.plot();
    }
}

希望这对您有用。