gpt4 book ai didi

java - 使用 JFreeChart 创建正态分布图

转载 作者:行者123 更新时间:2023-12-01 04:36:38 25 4
gpt4 key购买 nike

我正在尝试使用 JFreeChart 库制作正态分布图。如果我尝试获得图表下方的一个区域,我就会成功。但是我没有找到如何获取图表下的 2 个区域的方法。

这是一侧的代码:

public GrafHujungKanan() {

Function2D normal = new NormalDistributionFunction2D(0.0, 1.0);
dataset = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100,
"Normal");

XYSeries fLine = new XYSeries("fLine");
fLine.add(nilaiKritikal, 0);
fLine.add(4, 0);
((XYSeriesCollection) dataset).addSeries(fLine);

NumberAxis xAxis = new NumberAxis(null);
NumberAxis yAxis = new NumberAxis(null);
XYDifferenceRenderer renderer = new XYDifferenceRenderer();
xAxis.setRange(0, 5);
plot = new XYPlot(dataset, xAxis, yAxis, renderer);

chart = new JFreeChart(plot);
chart.removeLegend();

ChartPanel cp = new ChartPanel(chart);
this.add(cp);
}

上面的代码是这样的

Here is how it looks with the above code

这就是我需要的样子:

enter image description here

我已经尝试过用正值和负值翻转值。但图表的线条变成了绿色。

这是我尝试过的

public GrafDuaHujung() {

Function2D normal = new NormalDistributionFunction2D(0.0, 1.0);
dataset = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100,
"Normal");

// line on right side
XYSeries fLine = new XYSeries("fLine");
fLine.add(2, 0);
fLine.add(4, 0);
((XYSeriesCollection) dataset).addSeries(fLine);

// line on left side
XYSeries dLine = new XYSeries("dLine");
dLine.add(-2, 0);
dLine.add(-4, 0);
((XYSeriesCollection) dataset).addSeries(dLine);

NumberAxis xAxis = new NumberAxis(null);
NumberAxis yAxis = new NumberAxis(null);
XYDifferenceRenderer renderer = new XYDifferenceRenderer();
xAxis.setRange(0, 5);
plot = new XYPlot(dataset, xAxis, yAxis, renderer);

chart = new JFreeChart(plot);
chart.removeLegend();

ChartPanel cp = new ChartPanel(chart);
this.add(cp);
}

感谢您的回答。

最佳答案

您可以使用多个数据集。

public GrafDuaHujung() {

Function2D normal = new NormalDistributionFunction2D(0.0, 1.0);
dataset = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100, "Normal");
dataset2 = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100, "Normal"); //New

// line on right side
XYSeries fLine = new XYSeries("fLine");
fLine.add(2, 0);
fLine.add(4, 0);
((XYSeriesCollection) dataset).addSeries(fLine);

// line on left side
XYSeries dLine = new XYSeries("dLine");
dLine.add(-2, 0);
dLine.add(-4, 0);
((XYSeriesCollection) dataset2).addSeries(dLine); //Changed

XYDifferenceRenderer renderer = new XYDifferenceRenderer();
plot = new XYPlot(); //New
plot.setDataset(0, dataset); //New
plot.setDataset(1, dataset2); //New
plot.setRenderer(renderer); //New

chart = new JFreeChart(plot);
chart.removeLegend();

ChartPanel cp = new ChartPanel(chart);
this.add(cp);
}

关于java - 使用 JFreeChart 创建正态分布图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17254737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com