Difference between revisions of "Scatter plot"

From JSXGraph Wiki
Jump to navigationJump to search
 
Line 31: Line 31:
 
     y_arr = [];
 
     y_arr = [];
 
for (i = 0; i < 1000; i++) {
 
for (i = 0; i < 1000; i++) {
x = Math.random() * 8 - 4;
+
    x = Math.random() * 8 - 4;
y = Math.random() * 8 - 4;
+
    y = Math.random() * 8 - 4;
  x_arr.push(x, x, NaN);
+
    x_arr.push(x, x, NaN);
  y_arr.push(y, y, NaN);
+
    y_arr.push(y, y, NaN);
 
}
 
}
  

Latest revision as of 13:56, 14 April 2020

Of course, scatter plots can be realized with JSXGraph *point* objects. But if a very large number of points have to be plotted it may be more efficient to (ab)use the *curve* object. Here is a neat little trick how to do this: each data point is stored twice in the coordinate arrays, followed by NaNs. The NaNs interrupt the line stroke. With strokeWidth you can control the size of the points. The following example creates 1000 random points between -4 and 4 (in both directions):

The underlying JavaScript code

const board = JXG.JSXGraph.initBoard('jxgbox', { 
    boundingbox: [-5, 5, 5, -5], axis:true
});

var i, x, y, 
    x_arr = [], 
    y_arr = [];
for (i = 0; i < 1000; i++) {
    x = Math.random() * 8 - 4;
    y = Math.random() * 8 - 4;
    x_arr.push(x, x, NaN);
    y_arr.push(y, y, NaN);
}

var scatterplot = board.create('curve', [x_arr, y_arr], {strokeWidth: 3});