Java:非常简单的散点图实用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1856941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Really simple scatter plot utility
提问by Nick Heiner
I know there are many comparisons of java plotting libraries out there, but I'm not finding what I need. I just want a mind-numbingly simple toolkit that creates images of scatterplots from a set of coordinates. No GUI, no interaction, no fancy display, just a basic XY coordinate system with points.
我知道有很多 Java 绘图库的比较,但我没有找到我需要的。我只想要一个令人麻木的简单工具包,它可以根据一组坐标创建散点图图像。没有图形用户界面,没有交互,没有花哨的显示,只是一个带有点的基本 XY 坐标系。
It wouldn't be the end of the world to use something that offers a lot more functionality than I need, but I'd rather not. Do you know of anything like what I'm looking for?
使用提供比我需要的更多功能的东西并不是世界末日,但我宁愿不要。你知道我在找什么吗?
回答by JasCav
Have you looked at JFreeChart? While it can do some very advanced things, it also does the simple as well. Shown below is a screenshot of its scatter plot capability.
你看过JFreeChart吗?虽然它可以做一些非常高级的事情,但它也能做一些简单的事情。下面显示的是其散点图功能的屏幕截图。

(source: jfree.org)

(来源:jfree.org)
回答by Hugh Perkins
I looked around at what existed, and realized that jcckit is technically pretty good, but just lacks a simple wrapper around it to make it easy to use.
我环顾四周,发现 jcckit 在技术上非常好,但只是缺少一个简单的包装器来使其易于使用。
So I forked it and made a really simple wrapper. Here's how to use:
所以我分叉了它并制作了一个非常简单的包装器。以下是使用方法:
import static easyjcckit.QuickPlot.*;
double[] xaxis = new double[]{0,1,2,3,4,5};
double[] yvalues = new double[]{0,1,4,9,16,25};
scatter( xaxis, yvalues ); // create a plot using xaxis and yvalues
double[] yvalues2 = new double[]{0,1,2,3,4,5};
addScatter( xaxis, yvalues2 ); // create a second plot on top of first
System.out.println("Press enter to exit");
System.in.read();
As well as scatter plots, you can freely add lines to the same axes if you wish using 'addPlot' and 'plot'.
除了散点图,如果您希望使用 'addPlot' 和 'plot',您可以自由地在相同的轴上添加线。
Here is the code: https://bitbucket.org/hughperkins/easyjcckit
回答by Pierre
You an use a custom JPanel to draw your data(not tested, but you get the idea...)
您可以使用自定义 JPanel 来绘制数据(未经测试,但您明白了……)
private List<Point2D> data=(...);
JPanel pane=new JPanel()
{
protected paintComponent(Graphics2D g)
{
super.paintComponent(g);
int minx=(...),miny=(...),maxx=(...),maxy=(...);
for(Point2D p: data)
{
int x=((p.getX()-minx)/(maxx-minx))*this.getWidth();
int y=((p.getY()-miny)/(maxy-miny))*this.getHeight();
g.drawLine(x-5,y,x+5,y);
g.drawLine(x,y-5,x,y+5);
}
}
pane.setOpaque(true);
(...)
anotherComponent.add(pane);
(...)
}
回答by yuriy-g
Also you could check Simple Java Plot. Minimal example (no options):
你也可以检查Simple Java Plot。最小示例(无选项):
Plot plot = Plot.plot(null).
// setting data
series(null, Plot.data().
xy(1, 2).
xy(3, 4), null);
// saving sample_minimal.png
plot.save("sample_minimal", "png");

