Remove Trace from Plot in JavaScript

How to remove a trace from a plot in JavaScript with D3.js.


New to Plotly?

Plotly is a free and open-source graphing library for JavaScript. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

function plotGraph(){
var trace1 = {
  x: [1, 2, 3, 4],
  y: [10, 15, 13, 17],
  type: 'scatter',
  line: {
    color: 'rgb(55, 128, 191)',
  }
};

var trace2 = {
  x: [1, 2, 3, 4],
  y: [16, 5, 11, 9],
  type: 'scatter',
  line: {
    color: 'rgb(255,140,0)',
  }
};

var layout = {
  title:'Click Buttons to Delete Traces',
  showlegend:false
};

var data = [trace1, trace2];

Plotly.newPlot('myDiv', data, layout);
}

function deleteTrace(divId){
  Plotly.deleteTraces('myDiv', 0);
};