Reputation: 10433
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:
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:
Upvotes: 1
Views: 410
Reputation: 840
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:
Upvotes: 1