I'm trying to plot a surface plot of a function by generating the points myself via a for loop. The graph however doesn't show up. I've read that the z entry in the data variable for surface plots has to be an array of arrays but even that hasn't made the plot show up.
var zPts = [];
var xPts = [];
var yPts = [];
for(x=0; x<1; x+=0.01) {
for (y=0; y<1; y+=0.01) {
zPts.push([Math.sin(x*y)]);
yPts.push(y);
xPts.push(x);
}
}
var data = [{
z: zPts,
x: xPts,
y: yPts,
type: 'surface'
}];
var layout = {
title: 'My Plot',
autosize: false,
width: 500,
height: 500,
margin: {
l: 65,
r: 50,
b: 65,
t: 90,
}
};
Plotly.newPlot('theDiv', data, layout);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="theDiv"></div>
After some messing around I've found that the following code produces the plots needed with only the addition of 3 temporary array variables needed. I believe the correct formatting for the data is that the x, y and z arrays have to all be an array of arrays with each sub array being the same size for all 3.
var zPts = [];
var xPts = [];
var yPts = [];
for(x=0; x<10; x+=0.1) {
let zTemp = [];
let yTemp = [];
let xTemp = [];
for (y=0; y<10; y+=0.1) {
zTemp.push(Math.sin(x)*Math.cos(y));
yTemp.push(y);
xTemp.push(x);
}
zPts.push(zTemp);
yPts.push(yTemp);
xPts.push(xTemp);
}
var data = [{
z: zPts,
x: xPts,
y: yPts,
type: 'surface'
}];
var layout = {
title: 'My Plot',
autosize: false,
width: 500,
height: 500,
margin: {
l: 65,
r: 50,
b: 65,
t: 90,
}
};
Plotly.newPlot('theDiv', data, layout);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="theDiv"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With