How would I create a JFreeChart scatterplot best fit line

三世轮回 提交于 2019-12-04 05:31:41

问题


I have an arraylist of points I want to include in a JFreeChart scatterplot. That works fine, but I now want a best fit line on it. After some searching, JFreeChart doesn't support such calculations directly, so what I want to do is calculate it myself and then stick a line into the chart manually. How do I get a line in a scatterplot?

        XYSeries series = new XYSeries("Data");
        for (Point p : points) {
           series.add(p.getX(), p.getY());
        }
        XYSeriesCollection dataset = new XYSeriesCollection(series);
        JFreeChart chart = ChartFactory.createScatterPlot(chartName, "Mass", parameter, dataset, PlotOrientation.VERTICAL, false, true, true);
        return chart;

回答1:


Use the built-in Regression method getOLSRegression() or a statistical library such as Apache Commons Math to determine the slope and intercept of such a line using simple regression. Add your original data to a scatter plot, as shown here. Add an XYLineAnnotation representing the endpoints of your line, as shown here.



来源:https://stackoverflow.com/questions/6652319/how-would-i-create-a-jfreechart-scatterplot-best-fit-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!