gpt4 book ai didi

java - 数轴 setTickUnit 包含指定数

转载 作者:行者123 更新时间:2023-11-30 10:37:22 25 4
gpt4 key购买 nike

我正在使用 JFreeChart 显示正态分布,并将刻度数更改为标准差;但我也希望在滴答声出现的中间始终存在“平均”值(value)。交叉发布here .

所以标准偏差 = 2 ;均值 = 1

-3 -1  1 3 5

标准偏差 = 5;均值 = 15

0 5 10 15 20 25 30

JFreeChart.java

public class JFreeChartPanel extends JPanel {
private final XYPlot plot;
double mean = 0.0, sd = 1.0;
XYDataset dataset = initDataset();
NumberAxis domain = new NumberAxis("Y") {
@Override
protected double calculateLowestVisibleTickValue() {
double lowTickValue = super.calculateLowestVisibleTickValue();
if (mean % 2 == 1) {
return lowTickValue + 1;
} else {
return lowTickValue;
}
}
};
public JFreeChartPanel(){
JFreeChart chart = ChartFactory.createXYLineChart(
"Normal Distribution",
"X",
"PDF",
dataset,
PlotOrientation.VERTICAL,
false,
false,
false
);
plot=chart.getXYPlot();
domain.setAutoRangeStickyZero(false);
domain.setTickUnit(new NumberTickUnit(sd));
plot.setDomainAxis(domain);
final ChartPanel chartPanel = new ChartPanel(chart);
setLayout(new BorderLayout());
add(chartPanel);
}

private XYDataset initDataset() {
double minX=mean-(4*sd),maxX=mean+(4*sd);
Function2D normal = new NormalDistributionFunction2D(mean, sd);
XYDataset dataset = DatasetUtilities.sampleFunction2D(normal, minX, maxX, 100, "Normal");
return dataset;
}

public double getMean() {
return mean;
}

public void setMean(double mean) {
this.mean = mean;
plot.setDataset(initDataset());
}

public double getSd() {
return sd;
}

public void setSd(double sd) {
this.sd = sd;
domain.setTickUnit(new NumberTickUnit(sd));
plot.setDataset(initDataset());
}
}

用户界面.java

public class UI extends javax.swing.JFrame {

public UI() {
initComponents();
}

@SuppressWarnings("unchecked")
private void initComponents() {
Auto-generated Netbeans GUI COde
}

public void updateMean()
{
try{
double m = Double.parseDouble(mean.getText());
jFreeChartPanel.setMean(m);
}catch(Exception e){
}
}
public void updateSd()
{
try{
double sd = Double.parseDouble(standardDeviation.getText());
jFreeChartPanel.setSd(sd);
}catch(Exception e){
}
}

public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new UI().setVisible(true);
}
});
}

private javax.swing.JPanel inputPanel;
private main.JFreeChartPanel jFreeChartPanel;
private javax.swing.JPanel jPanel1;
private javax.swing.JToggleButton jToggleButton1;
private javax.swing.JTextField mean;
private javax.swing.JLabel meanLabel;
private javax.swing.JTextField standardDeviation;
private javax.swing.JLabel standardDeviationLabel;
}

最佳答案

从此example开始,我根据您的修改进行了以下更改,以获得如下所示的 µ=15 和 σ=5 的结果:

import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;

private double mean = 15.0, sigma = 5.0;

private XYDataset initDataset() {
double minX = mean - (3 * sigma), maxX = mean + (3 * sigma);
Function2D normal = new NormalDistributionFunction2D(mean, sigma);
XYDataset dataset = DatasetUtilities.sampleFunction2D(normal, minX, maxX, 100, "Normal");
return dataset;
}

public JFreeChartPanel() {

plot = chart.getXYPlot();
NumberAxis domain = (NumberAxis) plot.getDomainAxis();
domain.setTickUnit(new NumberTickUnit(sigma));

}

µ=15; σ=5

您的另一种情况,µ=1 和 σ=2,需要覆盖 NumberAxis 方法 calculateLowestVisibleTickValue() 以使刻度落在奇数上。

NumberAxis domain = new NumberAxis("Y") {
@Override
protected double calculateLowestVisibleTickValue() {
double lowTickValue = super.calculateLowestVisibleTickValue();
if (mean % 2 == 1) {
return lowTickValue + 1;
} else {
return lowTickValue;
}
}
};
domain.setTickUnit(new NumberTickUnit(sigma));
plot = chart.getXYPlot();
plot.setDomainAxis(domain);

µ=1; σ=2

Why [is] the graph is slightly skewed for mean 3 and sd 1 also for mean 6 sd 2, mean 9 sd 3 and so on; here is a picture showing this.

讨论 here , 有一个标志,当范围被自动确定时,它会影响添加到轴范围的边距的大小。

domain.setAutoRangeStickyZero(false);

I've added…code.

添加和实例化您的 revision 6 JFreeChartPanel 到 µ=1 和 σ=2 的 JFrame 产生以下结果:

enter image description here

This happens with higher mean.

This

您可以从自动范围中排除零。

exclude zero

domain.setAutoRangeIncludesZero(false);

关于java - 数轴 setTickUnit 包含指定数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40184461/

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