Using an array variable to generate plots

I am trying to use the function given on the plotly website but use a javascript array instead of manually entering values. The array would have different length based on the run. Can anyone help comment on a way of doing that? Tried substituting the matrix for x/y axes below and it does not generate the plot.

var trace2 = {
x: [2, 3, 4, 5],
y: [16, 5, 11, 9],
mode: ‘lines’
};

var trace3 = {
x: [1, 2, 3, 4],
y: [12, 9, 15, 12],
mode: ‘lines+markers’
};

var data = [ trace1, trace2, trace3 ];

var layout = {
title:‘Line and Scatter Plot’
};

Plotly.newPlot(‘myDiv’, data, layout);

You mean something like this:

var x2 = [2, 3, 4, 5];
var y2 =  [16, 5, 11, 9];

var x3 = [1, 2, 3, 4];
var y3 =  [12, 9, 15, 12];

var trace2 = {
  x: x2,
  y: y2,
  mode: ‘lines’
};

var trace3 = {
  x: x3,
  y: y3,
  mode: ‘lines+markers’
};

var data = [ trace1, trace2, trace3 ];

var layout = {
  title:‘Line and Scatter Plot’
};

Plotly.newPlot(‘myDiv’, data, layout);
1 Like