如何在JavaPlot中输出png文件 [英] How make output png file in JavaPlot

查看:78
本文介绍了如何在JavaPlot中输出png文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i using ">gnujavaplot and i not found a way to make outputs image files. I just plot in gnuplot window, i need the plot output in png file.

我需要类似的东西: $设置终端png $设置输出"filename.png" 在gnujavaplot中.

I need something like: $ a set terminal png $ set output "filename.png" in gnujavaplot.

谢谢.

推荐答案

我只是复制了我的实际代码,没有进行任何编辑,所以有一些开销,但是我认为您应该有所作为.有趣的部分始于

i just copy my actual code, withoout editing, so there is a few overhead, but i think you should get the thing. The interesting part starts at

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);
    }
}

}

这篇关于如何在JavaPlot中输出png文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆