Rod
Rod

Reputation: 752

Plotly Pie Chart and label order

How to change the order of the labels in the Pie Chart (plotly)?

I want to force this order: 20 16 15

And not 16 15 20


My csv file:

id,A,B,C
1,15,16,45
2,20,15,54
3,16,18,60
4,16,15,54
5,15,12,68
6,16,20,68

My python code

import pandas
import plotly.graph_objects as go

col_label = "A"
col_values = "Count"

data = pandas.read_csv(mycsvfile)
v = data[col_label].value_counts()
new = pandas.DataFrame({
    col_label: v.index,
    col_values: v.values
})
fig = go.Figure(
    data=[go.Pie(
        labels=new[col_label],
        values=new[col_values])
    ])
fig.show()

Gives this graph: graph

Upvotes: 10

Views: 14750

Answers (4)

Nicolas Bodin
Nicolas Bodin

Reputation: 1591

For people who have the same question in React (javascript), add sort: false to the data props of your Plot component:

const data = [{
    values: myValues,
    labels: myLabels,
    sort: false
}];

return (
    <Plot data={data} />
)

Upvotes: 1

puchal
puchal

Reputation: 2381

The legend order will be corresponding to order in labels (unless the sort = True in a chart which is True by default). What you have to do is to order the 'A' values in descending order and then to create a plot with adding parameter sort=False

import pandas
import plotly.graph_objects as go

col_label = "A"
col_values = "B"

data = pandas.read_csv(mycsvfile)
v = data[col_label].value_counts()
new = pandas.DataFrame({
    col_label: v.index,
    col_values: v.values
})
new = new.sort_values('A', ascending=False)

fig = go.Figure(
    data=[go.Pie(
        labels=new[col_label],
        values=new[col_values],
        sort=False
        )
    ])
fig.show()

Upvotes: 1

Valdrinium
Valdrinium

Reputation: 1416

There's 2 things:

import pandas
import plotly.graph_objects as go

col_label = "A"
col_values = "Count"

data = pandas.read_csv("mycsvfile")
v = data[col_label].value_counts()
new = pandas.DataFrame({
    col_label: v.index,
    col_values: v.values
})
# First, make sure that the data is in the order you want it to be prior to plotting 
new = new.sort_values(
  by=col_label, 
  ascending=False)

fig = go.Figure(
    data=[go.Pie(
        labels=new[col_label],
        values=new[col_values],
        # Second, make sure that Plotly won't reorder your data while plotting
        sort=False)
    ])
fig.write_html('first_figure.html', auto_open=False)

See this Repl.it for a working demo (it produces the html page with the plot).

Upvotes: 16

wizofe
wizofe

Reputation: 494

Use the layout.legend.traceorder attribute such as:

traceorder (flaglist string) 

Any combination of "reversed", "grouped" joined with a "+" OR "normal". 
examples: "reversed", "grouped", "reversed+grouped", "normal" 
Determines the order at which the legend items are displayed. 

If "normal", the items are displayed top-to-bottom in the same order 
as the input data. If "reversed", the items are displayed in the opposite order 
as "normal". If "grouped", the items are displayed in groups (when a trace
`legendgroup` is provided).  If "grouped+reversed", the items are displayed in the 
opposite order as "grouped".

See more in the official documentation.

Upvotes: 1

Related Questions