6
votes

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

2
Welcome to stack overflow! When you have a code example which isn't working for you, it is important to explain very clearly what the problem is -- what exactly is it doing right now, and what is it exactly that it should be doing. This is not clear from your question right now. - Chris Beck
Problem: colors aren't changing ('marker' has no effect) - Question: is there a different way I'm supposed to modify the colors - Zach Kramer
Ok, you should edit that into the question then - Chris Beck
Your example works for me. Here is the plot url: plot.ly/~christopherp/1131/grades-in-gym. Is that different than what you get? - Chris P
Hmm..the first 4 (v0-v3) seem to be the correct colors, but the rest are all wrong. For reference, the last 3 should be black. Perhaps there isn't a way yet. - Zach Kramer

2 Answers

5
votes

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>
1
votes

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.