Carpet Contour Plot in JavaScript

How to make D3.js-based carpet contour plots in Plotly.js.


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.

var trace1  = {
    type: 'carpet',
    a: [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],
    b: [4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6],
    x: [2, 3, 4, 5, 2.2, 3.1, 4.1, 5.1, 1.5, 2.5, 3.5, 4.5],
    y: [1, 1.4, 1.6, 1.75, 2, 2.5, 2.7, 2.75, 3, 3.5, 3.7, 3.75],
    aaxis: {
      tickprefix: "a = ",
      smoothing: 0,
      minorgridcount: 9,
      type: 'linear'
},
    baxis: {
      tickprefix: "b = ",
      smoothing: 0,
      minorgridcount: 9,
      type: 'linear'
    }
}

var layout = {
    title: {
      text: "Cheater plot with 1d input"
    },
    margin: {
      t: 40, r: 30, b: 30, l: 30
    },
    yaxis: {
      range: [0.388,4.361]
    },
    xaxis: {
      range: [0.667,5.932]
    }
}

var data = [trace1]

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 type: 'carpet',\n a: [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],\n b: [4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6],\n x: [2, 3, 4, 5, 2.2, 3.1, 4.1, 5.1, 1.5, 2.5, 3.5, 4.5],\n y: [1, 1.4, 1.6, 1.75, 2, 2.5, 2.7, 2.75, 3, 3.5, 3.7, 3.75],\n aaxis: {\n tickprefix: \"a = \",\n smoothing: 0,\n minorgridcount: 9,\n type: 'linear'\n},\n baxis: {\n tickprefix: \"b = \",\n smoothing: 0,\n minorgridcount: 9,\n type: 'linear'\n }\n}\n\nvar layout = {\n title: {\n text: \"Cheater plot with 1d input\"\n },\n margin: {\n t: 40, r: 30, b: 30, l: 30\n },\n yaxis: {\n range: [0.388,4.361]\n },\n xaxis: {\n range: [0.667,5.932]\n }\n}\n\nvar data = [trace1]\n\nPlotly.newPlot('myDiv', data, layout)\n"}">
var trace1 = {
    type: 'contourcarpet',
    a: [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],
    b: [4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6],
    z: [1, 1.96, 2.56, 3.0625, 4, 5.0625, 1, 7.5625, 9, 12.25, 15.21, 14.0625],
    autocontour: false,
    contours: {
      start: 1,
      end: 14,
      size: 1
    },
    line: {
      width: 2,
      smoothing: 0
    },
    colorbar: {
      len: 0.4,
      y: 0.25
    }
}

var trace2  = {
    type: 'carpet',
    a: [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],
    b: [4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6],
    x: [2, 3, 4, 5, 2.2, 3.1, 4.1, 5.1, 1.5, 2.5, 3.5, 4.5],
    y: [1, 1.4, 1.6, 1.75, 2, 2.5, 2.7, 2.75, 3, 3.5, 3.7, 3.75],
    aaxis: {
      tickprefix: "a = ",
      smoothing: 0,
      minorgridcount: 9,
      type: 'linear'
},
    baxis: {
      tickprefix: "b = ",
      smoothing: 0,
      minorgridcount: 9,
      type: 'linear'
    }
}

var layout = {
    title: {
      text: "Cheater plot with 1d input"
    },
    margin: {
      t: 40, r: 30, b: 30, l: 30
    },
    yaxis: {
      range: [0.388,4.361]
    },
    xaxis: {
      range: [0.667,5.932]
    }
}

var data = [trace1,trace2]

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 type: 'contourcarpet',\n a: [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],\n b: [4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6],\n z: [1, 1.96, 2.56, 3.0625, 4, 5.0625, 1, 7.5625, 9, 12.25, 15.21, 14.0625],\n autocontour: false,\n contours: {\n start: 1,\n end: 14,\n size: 1\n },\n line: {\n width: 2,\n smoothing: 0\n },\n colorbar: {\n len: 0.4,\n y: 0.25\n }\n}\n\nvar trace2 = {\n type: 'carpet',\n a: [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3],\n b: [4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6],\n x: [2, 3, 4, 5, 2.2, 3.1, 4.1, 5.1, 1.5, 2.5, 3.5, 4.5],\n y: [1, 1.4, 1.6, 1.75, 2, 2.5, 2.7, 2.75, 3, 3.5, 3.7, 3.75],\n aaxis: {\n tickprefix: \"a = \",\n smoothing: 0,\n minorgridcount: 9,\n type: 'linear'\n},\n baxis: {\n tickprefix: \"b = \",\n smoothing: 0,\n minorgridcount: 9,\n type: 'linear'\n }\n}\n\nvar layout = {\n title: {\n text: \"Cheater plot with 1d input\"\n },\n margin: {\n t: 40, r: 30, b: 30, l: 30\n },\n yaxis: {\n range: [0.388,4.361]\n },\n xaxis: {\n range: [0.667,5.932]\n }\n}\n\nvar data = [trace1,trace2]\n\nPlotly.newPlot('myDiv', data, layout)\n"}">
function Get(url){
    var Httpreq = new XMLHttpRequest();
    Httpreq.open("GET",url,false);
    Httpreq.send(null);
    return Httpreq.responseText;
}

var json_obj = JSON.parse(Get("https://raw.githubusercontent.com/bcdunbar/datasets/master/airfoil_data.json"));

var trace1 = {
      a: json_obj[0].a,
      b: json_obj[0].b,
      baxis: {
        startline: false,
        endline: false,
        showticklabels: "none",
        smoothing: 0,
        showgrid: false
      },
      x: json_obj[0].x,
      y: json_obj[0].y,
      type: "carpet",
      aaxis:{
        startlinewidth: 2,
        startline: true,
        showticklabels: "none",
        endline: true,
        showgrid: false,
        endlinewidth: 2,
        smoothing: 0
      }
    }

 var trace2 = {
      autocolorscale: false,
      zmax: 1,
      name: "Pressure",
      colorscale: "Viridis",
      zmin: -8,
      colorbar: {
        y: 0,
        yanchor: "bottom",
        title: {side:
          'right'
        },
        len: 0.75,
        title: {
          text: "Pressure coefficient, c<sub>p</sub>"
        },
      },
      contours: {
        start: -1,
        size: 0.025,
        end: 1.000,
        showlines: false
      },
      line: {
        smoothing: 0
      },
      z: json_obj[1].z,
      type: "contourcarpet",
      autocontour: false,
      zauto: false
    }

 var trace3 = {
      opacity: 0.300,
      showlegend: true,
      name: "Streamlines",
      autocontour: true,
      ncontours: 50,
      contours: {
        coloring: "none"
      },
      line: {
        color: "white",
        width: 1
      },
      z: json_obj[2].z,
      type: "contourcarpet"
    }

 var trace4 = {
      showlegend: true,
      name: "Pressure<br>contours",
      autocontour: false,
      z: json_obj[3].z,
      type: "contourcarpet",
      line: {
        color: "rgba(0, 0, 0, 0.5)",
        smoothing: 1
      },
      contours: {
        size: 0.250,
        start: -4,
        coloring: "none",
        end: 1.000,
        showlines: true
      }
    }

 var trace5 = {
      legendgroup: "g1",
      name: "Surface<br>pressure",
      mode: "lines",
      hoverinfo: "skip",
      x: json_obj[4].x,
      y: json_obj[4].y,
      line: {
        color: "rgba(255, 0, 0, 0.5)",
        width: 1,
        shape: "spline",
        smoothing: 1
      },
      fill: "toself",
      type: "scatter",
      fillcolor: "rgba(255, 0, 0, 0.2)"
    }

 var trace6 = {
      showlegend: false,
      legendgroup: "g1",
      mode: "lines",
      hoverinfo: "skip",
      x: json_obj[5].x,
      y: json_obj[5].y,
      line: {
        color: "rgba(255, 0, 0, 0.3)",
        width: 1
      },
      type: "scatter"
    }

 var trace7 = {
      showlegend: false,
      legendgroup: "g1",
      name: "cp",
      text: json_obj[6].text,
      mode: "lines",
      hoverinfo: "text",
      x: json_obj[6].x,
      y: json_obj[6].y,
      line: {
        color: "rgba(255, 0, 0, 0.2)",
        width: 0
      },
      type: "scatter"
    }

data = [trace1,trace2,trace3,trace4,trace5,trace6,trace7]

var layout = {
    yaxis: {
      zeroline: false,
      range: [-1.800,1.800],
      showgrid: false
    },
    dragmode: "pan",
    height: 700,
    xaxis: {
      zeroline: false,
      scaleratio: 1,
      scaleanchor: "y",
      range: [-3.800,3.800],
      showgrid: false
    },
    title: {
      text: "Flow over a Karman-Trefftz airfoil"
    },
    hovermode: "closest",
    margin: {
      r: 60,
      b: 40,
      l: 40,
      t: 80
    },
    width: 900
  }

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":"function Get(url){\n var Httpreq = new XMLHttpRequest();\n Httpreq.open(\"GET\",url,false);\n Httpreq.send(null);\n return Httpreq.responseText;\n}\n\nvar json_obj = JSON.parse(Get(\"https://raw.githubusercontent.com/bcdunbar/datasets/master/airfoil_data.json\"));\n\nvar trace1 = {\n a: json_obj[0].a,\n b: json_obj[0].b,\n baxis: {\n startline: false,\n endline: false,\n showticklabels: \"none\",\n smoothing: 0,\n showgrid: false\n },\n x: json_obj[0].x,\n y: json_obj[0].y,\n type: \"carpet\",\n aaxis:{\n startlinewidth: 2,\n startline: true,\n showticklabels: \"none\",\n endline: true,\n showgrid: false,\n endlinewidth: 2,\n smoothing: 0\n }\n }\n\n var trace2 = {\n autocolorscale: false,\n zmax: 1,\n name: \"Pressure\",\n colorscale: \"Viridis\",\n zmin: -8,\n colorbar: {\n y: 0,\n yanchor: \"bottom\",\n title: {side:\n 'right'\n },\n len: 0.75,\n title: {\n text: \"Pressure coefficient, c<sub>p</sub>\"\n },\n },\n contours: {\n start: -1,\n size: 0.025,\n end: 1.000,\n showlines: false\n },\n line: {\n smoothing: 0\n },\n z: json_obj[1].z,\n type: \"contourcarpet\",\n autocontour: false,\n zauto: false\n }\n\n var trace3 = {\n opacity: 0.300,\n showlegend: true,\n name: \"Streamlines\",\n autocontour: true,\n ncontours: 50,\n contours: {\n coloring: \"none\"\n },\n line: {\n color: \"white\",\n width: 1\n },\n z: json_obj[2].z,\n type: \"contourcarpet\"\n }\n\n var trace4 = {\n showlegend: true,\n name: \"Pressure<br>contours\",\n autocontour: false,\n z: json_obj[3].z,\n type: \"contourcarpet\",\n line: {\n color: \"rgba(0, 0, 0, 0.5)\",\n smoothing: 1\n },\n contours: {\n size: 0.250,\n start: -4,\n coloring: \"none\",\n end: 1.000,\n showlines: true\n }\n }\n\n var trace5 = {\n legendgroup: \"g1\",\n name: \"Surface<br>pressure\",\n mode: \"lines\",\n hoverinfo: \"skip\",\n x: json_obj[4].x,\n y: json_obj[4].y,\n line: {\n color: \"rgba(255, 0, 0, 0.5)\",\n width: 1,\n shape: \"spline\",\n smoothing: 1\n },\n fill: \"toself\",\n type: \"scatter\",\n fillcolor: \"rgba(255, 0, 0, 0.2)\"\n }\n\n var trace6 = {\n showlegend: false,\n legendgroup: \"g1\",\n mode: \"lines\",\n hoverinfo: \"skip\",\n x: json_obj[5].x,\n y: json_obj[5].y,\n line: {\n color: \"rgba(255, 0, 0, 0.3)\",\n width: 1\n },\n type: \"scatter\"\n }\n\n var trace7 = {\n showlegend: false,\n legendgroup: \"g1\",\n name: \"cp\",\n text: json_obj[6].text,\n mode: \"lines\",\n hoverinfo: \"text\",\n x: json_obj[6].x,\n y: json_obj[6].y,\n line: {\n color: \"rgba(255, 0, 0, 0.2)\",\n width: 0\n },\n type: \"scatter\"\n }\n\ndata = [trace1,trace2,trace3,trace4,trace5,trace6,trace7]\n\nvar layout = {\n yaxis: {\n zeroline: false,\n range: [-1.800,1.800],\n showgrid: false\n },\n dragmode: \"pan\",\n height: 700,\n xaxis: {\n zeroline: false,\n scaleratio: 1,\n scaleanchor: \"y\",\n range: [-3.800,3.800],\n showgrid: false\n },\n title: {\n text: \"Flow over a Karman-Trefftz airfoil\"\n },\n hovermode: \"closest\",\n margin: {\n r: 60,\n b: 40,\n l: 40,\n t: 80\n },\n width: 900\n }\n\nPlotly.newPlot('myDiv', data, layout);\n"}">