Defining pie chart slice colors with Plotly

12,474

Solution 1

Plotly does pretty well with hiding your errors and auto completing the missing colors. There are few missing commas, below a working example of colored pie chart.

As Meglio offered, the sort attribute can be set to false, but this only changes the order of the legend and the chart. The colors are set by the ordering of the arrays.

pie_chart = {
  'data': [{
    'labels': ['V0', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9'],
    'values': [55, 22, 31, 32, 33, 45, 44, 42, 12, 67],
    'marker': {
      'colors': [
        'rgb(0, 204, 0)',
        'rgb(255, 255, 0)',
        'rgb(118, 17, 195)',
        'rgb(0, 48, 240)',
        'rgb(240, 88, 0)',
        'rgb(215, 11, 11)',
        'rgb(11, 133, 215)',
        'rgb(0, 0, 0)',
        'rgb(0, 0, 0)',
        'rgb(0, 0, 0)'
      ]
    },
    'type': 'pie',
    'name': "Gym grades",
    'hoverinfo': 'label+percent+name',
    'sort': false,
  }],

  'layout': {
    'title': 'Grades in gym'
  }

}

Plotly.newPlot('plot', pie_chart.data, pie_chart.layout);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="plot"> </div>

Solution 2

The problem might be that by default all the values v0, v1, ... are sorted descending, so that the biggest values come first. They are sorted after "applying" colors to them. So, value #4, which is Blue as by your colors definition, may come not 4th.

The solution would be to order your data manually and turn auto-ordering off with using sort attribute.

Share:
12,474
Zach Kramer
Author by

Zach Kramer

Embedded systems developer with experience in Dev Ops, QA, and software engineering. Most of my work revolves around Python, C, and modern C++, from computational models to machine vision.

Updated on September 16, 2022

Comments

  • Zach Kramer
    Zach Kramer over 1 year

    I'm trying to create specific colors for certain values/slices, but can't find much documentation on it. I know it's possible for bar charts and such, even pie charts in Javascript, and one of their examples has a colors option, but modifying it does nothing for me.

    This is what I have:

    pie_chart = {
                 'data': 
                         [
                         {     'labels': ['V0', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6', 'V7', 'V8', 'V9'],
                               'values': [v0, v1, v2, v3, v4, v5, v6, v7, v8, v9],
                               'marker': {'colors': [
                                                     'rgb(0, 204, 0)',  # Green
                                                     'rgb(255, 255, 0)',  # Yellow
                                                     'rgb(118, 17, 195)',  # Purple
                                                     'rgb(0, 48, 240)'  # Blue
                                                     'rgb(240, 88, 0)'  # Orange
                                                     'rgb(215, 11, 11)'  # Red
                                                     'rgb(11, 133, 215)'  # Light Blue
                                                     'rgb(0, 0, 0)'  # Black
                                                     'rgb(0, 0, 0)'
                                                     'rgb(0, 0, 0)'
                                                    ]
                                         },
                               'type': 'pie',
                               'name': "Gym grades",
                               'hoverinfo':'label+percent+name',
                               'textinfo':'none'
                          }
                          ],
    
                 'layout': {'title': 'Grades in gym'}
    
                 }
    
        url = py.plot(pie_chart, validate=False, filename='Gym Grades')
    

    Problem: colors aren't changing ('marker' has no effect)

    Question: is there a different way I'm supposed to modify the colors

  • Zach Kramer
    Zach Kramer almost 6 years
    Classic mistake! Thanks