|
| 1 | +```java |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import java.awt.*; |
| 6 | + |
| 7 | +import javax.swing.*; |
| 8 | + |
| 9 | +import org.math.plot.*; |
| 10 | +import org.math.plot.plotObjects.*; |
| 11 | + |
| 12 | +import static java.lang.Math.*; |
| 13 | + |
| 14 | +import static org.math.array.StatisticSample.*; |
| 15 | + |
| 16 | +public class CustomPlotExample { |
| 17 | + public static void main(String[] args) { |
| 18 | + |
| 19 | + // define your data |
| 20 | + double[] x = randomNormal(1000, 0, 1); // 1000 random numbers from a normal (Gaussian) statistical law |
| 21 | + double[] y = randomUniform(1000, -3, 3); // 1000 random numbers from a uniform statistical law |
| 22 | + |
| 23 | + // create your PlotPanel (you can use it as a JPanel) |
| 24 | + Plot2DPanel plot = new Plot2DPanel(); |
| 25 | + |
| 26 | + // legend at SOUTH |
| 27 | + plot.addLegend("SOUTH"); |
| 28 | + |
| 29 | + // add the histogram (50 slices) of x to the PlotPanel |
| 30 | + plot.addHistogramPlot("Gaussian population", x, 50); |
| 31 | + |
| 32 | + // add the histogram (50 slices) of y to the PlotPanel in GREEN |
| 33 | + plot.addHistogramPlot("Uniform population", Color.RED, y, 50); |
| 34 | + |
| 35 | + // add a title |
| 36 | + BaseLabel title = new BaseLabel("...My nice plot...", Color.RED, 0.5, 1.1); |
| 37 | + title.setFont(new Font("Courier", Font.BOLD, 20)); |
| 38 | + plot.addPlotable(title); |
| 39 | + |
| 40 | + // change name of axes |
| 41 | + plot.setAxesLabels("<X>", "frequency"); |
| 42 | + |
| 43 | + // customize X axe |
| 44 | + // rotate light labels |
| 45 | + plot.getAxe(0).setLightLabelAngle(-PI / 4); |
| 46 | + // change axe title position relatively to the base of the plot |
| 47 | + plot.getAxe(0).setLabelPosition(0.5, -0.15); |
| 48 | + |
| 49 | + // customize Y axe |
| 50 | + // rotate light labels |
| 51 | + plot.getAxe(1).setLightLabelAngle(-PI / 4); |
| 52 | + // change axe title position relatively to the base of the plot |
| 53 | + plot.getAxe(1).setLabelPosition(-0.15, 0.5); |
| 54 | + // change axe title angle |
| 55 | + plot.getAxe(1).setLabelAngle(-PI / 2); |
| 56 | + |
| 57 | + // put the PlotPanel in a JFrame like a JPanel |
| 58 | + JFrame frame = new JFrame("a plot panel"); |
| 59 | + frame.setSize(600, 600); |
| 60 | + frame.setContentPane(plot); |
| 61 | + frame.setVisible(true); |
| 62 | + |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +``` |
0 commit comments