Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining pie chart slice colors with Plotly

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

like image 664
Zach Kramer Avatar asked Sep 06 '15 03:09

Zach Kramer


2 Answers

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>
like image 133
Idan Avatar answered Sep 21 '22 10:09

Idan


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.

like image 35
Meglio Avatar answered Sep 20 '22 10:09

Meglio