1

I have the following code for some pie charts:

import plotly.graph_objects as go
from plotly.subplots import make_subplots

labels = ['Oxygen','Hydrogen']
labels2 = ['Carbon_Dioxide','Nitrogen']
values = [4500, 2500]
values2 = [ 1053, 500]

fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])


fig.add_trace(go.Pie(labels=labels, values= values, name=""),
              1, 1)


fig.add_trace(go.Pie(labels=labels2, values= values, name=""),
              1, 2)
fig.update_traces(hole=.4, hoverinfo="label+percent+name")

title = "PERCENTAGE OF SHOTS ON YOUR TEAM"


fig.update_layout(
    title_text=title,
    # Add annotations in the center of the donut pies.
    annotations=[dict(text='', x=0.18, y=0.5, font_size=20, showarrow=False),
                 dict(text='', x=0.82, y=0.5, font_size=20, showarrow=False)])

fig.update_traces(textposition='inside', textinfo='percent+label')
fig.show()

It generates the following image:

enter image description here

Overall the plot is fine, but I need to specify the colors, e.g.

colors = [ '#00c600', '#66b3ff',"#1f77b4",'#d62728']

But it I add it as:

fig.update_traces(hole=.4, hoverinfo="label+percent+name",marker=dict(colors=colors))

The new code is:

labels = ['Oxygen','Hydrogen']
labels2 = ['Carbon_Dioxide','Nitrogen']
values = [4500, 2500]
values2 = [ 1053, 500]

fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])


fig.add_trace(go.Pie(labels=labels, values= values, name=""),
              1, 1)


fig.add_trace(go.Pie(labels=labels2, values= values, name=""),
              1, 2)


fig.update_traces(hole=.4, hoverinfo="label+percent+name",marker=dict(colors=colors))


title = "PERCENTAGE OF SHOTS ON YOUR TEAM"


fig.update_layout(
    title_text=title,
    # Add annotations in the center of the donut pies.
    annotations=[dict(text='', x=0.18, y=0.5, font_size=20, showarrow=False),
                 dict(text='', x=0.82, y=0.5, font_size=20, showarrow=False)])

fig.update_traces(textposition='inside', textinfo='percent+label')
fig.show()

The problem is that now it is using only 2 colors and I need to have one color for each value:

enter image description here

4
  • you know the solution as well Commented Jul 18, 2020 at 18:14
  • @DivyesshMaheshwari No, becuase that way is not easy to read, I not able to set colors for each item so I have 4 colors and it is easier to read. Commented Jul 18, 2020 at 18:35
  • You know how to set colors so make two more that will take much lesser time than getting answers Commented Jul 18, 2020 at 18:38
  • 1
    if you want to support I can write same in answer and you can upvote, Or if you need I can find colors for you Commented Jul 18, 2020 at 18:39

1 Answer 1

1

You can use column arguments in the update_traces method, this will work on figures with subplots.

import plotly.graph_objects as go
from plotly.subplots import make_subplots

#colors = [ '#00c600', '#66b3ff',"#1f77b4",'#d62728']
colors2 = [ "#1f77b4",'#d62728']
colors3 = [ '#00c600', '#66b3ff']


labels = ['Oxygen','Hydrogen']
labels2 = ['Carbon_Dioxide','Nitrogen']
values = [4500, 2500]
values2 = [ 1053, 500]

fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])


fig.add_trace(go.Pie(labels=labels, values= values, name=""),
              1, 1)


fig.add_trace(go.Pie(labels=labels2, values= values, name=""),
              1, 2)

fig.update_traces(hole=.4, hoverinfo="label+percent+name",marker=dict(colors=colors2),col=0)
fig.update_traces(hole=.4, hoverinfo="label+percent+name",marker=dict(colors=colors3),col=1)



title = "PERCENTAGE OF SHOTS ON YOUR TEAM"


fig.update_layout(
    title_text=title,
    # Add annotations in the center of the donut pies.
    annotations=[dict(text='', x=0.18, y=0.5, font_size=20, showarrow=False),
                 dict(text='', x=0.82, y=0.5, font_size=20, showarrow=False)])

fig.update_traces(textposition='inside', textinfo='percent+label')
fig.show()

The update_traces has a col/row parameter, there is also a selector parameter:

fig.update_traces(marker=dict(color="RoyalBlue"),
                  col=2)

References:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.