Search code examples
javascriptcolorsplotly

Plotly.js default colors list


How do I obtain the default colors list / palette for plots with multiple series? (e.g. bubble chart)


Solution

  • Plotly uses the same colors as in d3's scale.category10() function. After 10 colors the whole color scheme starts again from 0.

    The colors codes can be found in KDD's answer. The snippet below gives the d3 colors and takes the Plotly colors dynamically from a plot, i.e. even if the Plotly changes the color codes will be correct.

    var d3colors = Plotly.d3.scale.category10();
    var color_div = document.getElementById('colors');
    var data = [];
    
    for (var i = 0; i < 11; i += 1) {
      data.push({
        x: [i],
        y: [i + 1],
        name: i + ": Color: " + d3colors(i),
        type: 'bar'
      });
    }
    Plotly.plot('colors', data);
    
    var node;
    var textnode;
    for (var i = 0; i < 10; i += 1) {
      var color = d3colors(i);
      node = document.createElement("div");
      node.style.color = color;
      var text = "D3: " + color;
      textnode = document.createTextNode(text);
      node.appendChild(textnode);
      node.appendChild(document.createElement("br"));
      var rgb = Plotly.d3.selectAll('.point').selectAll('path')[i][0].style.fill;
      color = Plotly.d3.rgb(rgb).toString()
      var text = "Plotly: " + color + " ; " + rgb;
      textnode = document.createTextNode(text);
      node.appendChild(textnode);
      color_div.appendChild(node); 
    }
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <div id="colors"></div>