Error Bars in JavaScript

How to add error bars to a D3.js-based line, scatter, or bar chart. Seven examples of symmetric, asymmetric, horizontal, and colored error bars.


Plotly is known for its graphing libraries, but it's also a broader ecosystem for building interactive data apps, AI data analysis tools, and deploying and hosting Dash apps. Explore the full set of tools and platforms designed to help turn data into action. Discover Plotly.

var data = [
  {
    x: [0, 1, 2],
    y: [6, 10, 2],
    error_y: {
      type: 'data',
      array: [1, 2, 3],
      visible: true
    },
    type: 'scatter'
  }
];
Plotly.newPlot('myDiv', data);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var data = [\n {\n x: [0, 1, 2],\n y: [6, 10, 2],\n error_y: {\n type: 'data',\n array: [1, 2, 3],\n visible: true\n },\n type: 'scatter'\n }\n];\nPlotly.newPlot('myDiv', data);\n"}">
var trace1 = {
  x: ['Trial 1', 'Trial 2', 'Trial 3'],
  y: [3, 6, 4],
  name: 'Control',
  error_y: {
    type: 'data',
    array: [1, 0.5, 1.5],
    visible: true
  },
  type: 'bar'
};
var trace2 = {
  x: ['Trial 1', 'Trial 2', 'Trial 3'],
  y: [4, 7, 3],
  name: 'Experimental',
  error_y: {
    type: 'data',
    array: [0.5, 1, 2],
    visible: true
  },
  type: 'bar'
};
var data = [trace1, trace2];
var layout = {barmode: 'group'};
Plotly.newPlot('myDiv', data, layout);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var trace1 = {\n x: ['Trial 1', 'Trial 2', 'Trial 3'],\n y: [3, 6, 4],\n name: 'Control',\n error_y: {\n type: 'data',\n array: [1, 0.5, 1.5],\n visible: true\n },\n type: 'bar'\n};\nvar trace2 = {\n x: ['Trial 1', 'Trial 2', 'Trial 3'],\n y: [4, 7, 3],\n name: 'Experimental',\n error_y: {\n type: 'data',\n array: [0.5, 1, 2],\n visible: true\n },\n type: 'bar'\n};\nvar data = [trace1, trace2];\nvar layout = {barmode: 'group'};\nPlotly.newPlot('myDiv', data, layout);\n"}">
var data = [
  {
    x: [1, 2, 3, 4],
    y: [2, 1, 3, 4],
    error_x: {
      type: 'percent',
      value: 10
    },
    type: 'scatter'
  }
];
Plotly.newPlot('myDiv', data);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var data = [\n {\n x: [1, 2, 3, 4],\n y: [2, 1, 3, 4],\n error_x: {\n type: 'percent',\n value: 10\n },\n type: 'scatter'\n }\n];\nPlotly.newPlot('myDiv', data);\n"}">
var data = [
  {
    x: [1, 2, 3, 4],
    y: [2, 1, 3, 4],
    error_y: {
      type: 'data',
      symmetric: false,
      array: [0.1, 0.2, 0.1, 0.1],
      arrayminus: [0.2, 0.4, 1, 0.2]
    },
    type: 'scatter'
  }
];
Plotly.newPlot('myDiv', data);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var data = [\n {\n x: [1, 2, 3, 4],\n y: [2, 1, 3, 4],\n error_y: {\n type: 'data',\n symmetric: false,\n array: [0.1, 0.2, 0.1, 0.1],\n arrayminus: [0.2, 0.4, 1, 0.2]\n },\n type: 'scatter'\n }\n];\nPlotly.newPlot('myDiv', data);\n"}">
function linspace(a,b,n) {
  return d3.range(n).map(function(i){return a+i*(b-a)/(n-1);});
}
x_theo = linspace(-4, 4, 100)
sincx = Math.sin(x_theo) / x_theo
var x = [-3.8, -3.03, -1.91, -1.46, -0.89, -0.24, -0.0, 0.41, 0.89, 1.01, 1.91, 2.28, 2.79, 3.56]
var y = [-0.02, 0.04, -0.01, -0.27, 0.36, 0.75, 1.03, 0.65, 0.28, 0.02, -0.11, 0.16, 0.04, -0.15]

var trace1 = {
  x: x_theo,
  y: sincx,
  name: 'sinc(x)',
  type: 'scatter'
};
var trace2 = {
  x: x,
  y: y,
  mode: 'markers',
  name: 'measured',
  error_y: {
    type: 'constant',
    value: 0.1,
    color: '#85144B',
    thickness: 1.5,
    width: 3,
  },
  error_x: {
    type: 'constant',
    value: 0.2,
    color: '#85144B',
    thickness: 1.5,
    width: 3,
  },
  marker: {
    color: '#85144B',
    size: 8
  },
  type: 'scatter'
};
var data = [trace1, trace2];
Plotly.newPlot('myDiv', data, {});
</script>\n\t<script src="/?url=https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fd3%2F3.5.17%2Fd3.min.js"></script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"function linspace(a,b,n) {\n return d3.range(n).map(function(i){return a+i*(b-a)/(n-1);});\n}\nx_theo = linspace(-4, 4, 100)\nsincx = Math.sin(x_theo) / x_theo\nvar x = [-3.8, -3.03, -1.91, -1.46, -0.89, -0.24, -0.0, 0.41, 0.89, 1.01, 1.91, 2.28, 2.79, 3.56]\nvar y = [-0.02, 0.04, -0.01, -0.27, 0.36, 0.75, 1.03, 0.65, 0.28, 0.02, -0.11, 0.16, 0.04, -0.15]\n\nvar trace1 = {\n x: x_theo,\n y: sincx,\n name: 'sinc(x)',\n type: 'scatter'\n};\nvar trace2 = {\n x: x,\n y: y,\n mode: 'markers',\n name: 'measured',\n error_y: {\n type: 'constant',\n value: 0.1,\n color: '#85144B',\n thickness: 1.5,\n width: 3,\n },\n error_x: {\n type: 'constant',\n value: 0.2,\n color: '#85144B',\n thickness: 1.5,\n width: 3,\n },\n marker: {\n color: '#85144B',\n size: 8\n },\n type: 'scatter'\n};\nvar data = [trace1, trace2];\nPlotly.newPlot('myDiv', data, {});\n"}">
var data = [
  {
    x: [0, 1, 2],
    y: [6, 10, 2],
    error_y: {
      type: 'percent',
      value: 50,
      visible: true
    },
    type: 'scatter'
  }
];
Plotly.newPlot('myDiv', data);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var data = [\n {\n x: [0, 1, 2],\n y: [6, 10, 2],\n error_y: {\n type: 'percent',\n value: 50,\n visible: true\n },\n type: 'scatter'\n }\n];\nPlotly.newPlot('myDiv', data);\n"}">
var data = [
  {
    x: [1, 2, 3, 4],
    y: [2, 1, 3, 4],
    error_y: {
      type: 'percent',
      symmetric: false,
      value: 15,
      valueminus: 25
    },
    type: 'scatter'
  }
];
Plotly.newPlot('myDiv', data);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var data = [\n {\n x: [1, 2, 3, 4],\n y: [2, 1, 3, 4],\n error_y: {\n type: 'percent',\n symmetric: false,\n value: 15,\n valueminus: 25\n },\n type: 'scatter'\n }\n];\nPlotly.newPlot('myDiv', data);\n"}">