在散点图上绘制二次曲线作为 y 的函数

Plot a quadratic curve as a function of y on a scatterplot

我正在使用 JFreeCharts 在 Java 中绘制散点图。我正在使用 PolynomialFunction2D 在散点图上绘制二次回归线。

PolynomialFunction2D 可用于绘制定义为 x 函数的二次曲线,即 y = a0 + a1 * x + a2 * x^2 + ...

我想绘制一条二次曲线作为 y 的函数,即 x = a0 + a1 * y + a2 * y^2 + ... 在 JFreeCharts 中制作的散点图上,当给定系数时.

这可能吗?并且,如果是,可以使用什么方法。

您可以从函数创建一个 XYSeries,方法是在 y 上手动采样函数并计算相应的 x 值。得到的数据集可以添加到XYPlot,连同散点图的XYDataset

举个例子:

import java.awt.Dimension;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.function.Function2D;
import org.jfree.data.function.PolynomialFunction2D;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

public class FunctionOfYOverScatter
{
    public static void main(String[] args)
    {
        new FunctionOfYOverScatter();
    }

    public FunctionOfYOverScatter()
    {
        XYPlot plot = new XYPlot();

        XYDataset scatterPlotDataset = getScatterPlotDataset();
        plot.setDataset(0, scatterPlotDataset);
        plot.setRenderer(0, new XYLineAndShapeRenderer(false, true));
        plot.setDomainAxis(0, new NumberAxis("Scatterplot domain"));
        plot.setRangeAxis(0, new NumberAxis("Scatterplot range"));
        plot.mapDatasetToDomainAxis(0, 0);
        plot.mapDatasetToRangeAxis(0, 0);

        double minY = -2.0;
        double maxY = 2.0;
        XYDataset functionDataset = 
            getFunctionDataset(0.8, 0.5, 1.2, minY, maxY);
        plot.setDataset(1, functionDataset);
        plot.setRenderer(1, new XYLineAndShapeRenderer(true, false));

        JFreeChart chart = new JFreeChart("Function of Y over scatter plot",
            JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(500, 270));
        ApplicationFrame frame = new ApplicationFrame("Example");
        frame.setContentPane(chartPanel);
        frame.pack();
        frame.setVisible(true);
    }

    private XYDataset getFunctionDataset(
        double a0, double a1, double a2,
        double minY, double maxY)
    {
        double[] a = { a0, a1, a2 };
        Function2D p = new PolynomialFunction2D(a);
        XYSeries series = sampleFunctionOverY(p, minY, maxY, 100, "Function");
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        return dataset;
    }

    public static XYSeries sampleFunctionOverY(Function2D f, double start,
        double end, int samples, Comparable<?> seriesKey)
    {
        XYSeries series = new XYSeries(seriesKey, false);
        double step = (end - start) / (samples - 1);
        for (int i = 0; i < samples; i++)
        {
            double y = start + step * i;
            series.add(f.getValue(y), y);
        }
        return series;
    }


    private XYDataset getScatterPlotDataset()
    {
        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries data = new XYSeries("Scatterplot");
        data.add(3.0, 2.0);
        data.add(1.7, 1.0);
        data.add(2.0, -1.0);
        data.add(1.0, 1.8);
        dataset.addSeries(data);
        return dataset;
    }


}