Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotly python - fix colours for specific legend labels

I am making a pie chart that looks like this.

I want to make multiple pie charts for different sets of data and keep the colours fixed to the legend names, but when the order changes, the colour scheme follows the order.

Is there a way to pass a dict into the chart to fix the colours to specific items?

[enter image description here]

like image 306
AndyMoore Avatar asked May 28 '26 16:05

AndyMoore


1 Answers

You cannot pass a dictionary with your colors, but you can specify the colors manually, set sort to False and pass the values always in the same order, e.g.

import plotly

fig = {
    'data': [{'labels': ['Residential', 'Non-Residential', 'Utility'],
              'values': [19, 26, 55],
              'type': 'pie',
              'sort': False,
              'marker': {'colors': ['rgb(255, 0, 0)',
                                    'rgb(0, 255, 0)',
                                    'rgb(0, 0, 255)']
                        }
            }]
     }

fig = {
    'data': [{'labels': ['Residential', 'Non-Residential', 'Utility'],
              'values': [100, 10, 25],
              'type': 'pie',
              'sort': False,
              'marker': {'colors': ['rgb(255, 0, 0)',
                                    'rgb(0, 255, 0)',
                                    'rgb(0, 0, 255)']
                        }
            }]
     }
plotly.offline.plot(fig)

enter image description here

like image 141
Maximilian Peters Avatar answered May 30 '26 09:05

Maximilian Peters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!