1

I'm new to plotly, I have two different plots below (go.Pie & go.Bar) and I like to assign a color to each type (a: g) label as shown in the color_discrete_map. How do I include them in the codes for go.pie & go.bar? Thanks

Dataframe: enter image description here

    color_discrete_map = {'a':'rgb(42,9,4)', 
                              'b':'rgb(111,203,209)',
                              'c':'rgb(55,165,172),',
                              'd':'rgb(29,127,136)',
                              'e':'rgb(2,84,92)',
                              'f':'rgb(4,37,42)'}
    
    unit_mix_pie.add_trace(go.Pie(labels=df.index, values=df['type']), row=1, col=1)


unit_mix_bar.add_trace(go.Bar(x=df.index, y=round(df['type'],0), marker=dict(
        color=px.colors.qualitative.Pastel2, color_discrete_map=color_discrete_map,
        line=dict(color='#000000', width=2)
    )), row=1, col=1)
2
  • 1
    Please provide a minimal reproducible example. In your case, we should at least be able to see what your DataFrame looks like so we can understand what your code does, but ideally we would be able to see more of your code as well including the unit_mix_pie object and so on Commented Apr 8, 2021 at 6:25
  • Hi @DerekO, thanks for your comment. Please see the edited post. Thanks Commented Apr 8, 2021 at 11:55

1 Answer 1

3

I am a little confused as to the point of unit_mix_pie and unit_mix_bar being subplots with only one row and one column - you could instead define each of these objects as a plotly graph_object or a plotly express figure.

If you define unit_mix_pie using plotly express, you can directly pass your color_discrete_map as a parameter:

import pandas as pd
import plotly.express as px

df = pd.DataFrame(data={'type':list('abcdefg'), 'no':[50,100,200,300,400,500,600]})

## added another color for the type 'g'
color_discrete_map = {'a':'rgb(42,9,4)', 
                          'b':'rgb(111,203,209)',
                          'c':'rgb(55,165,172)',
                          'd':'rgb(29,127,136)',
                          'e':'rgb(2,84,92)',
                          'f':'rgb(4,37,42)',
                          'g':'purple'}

unit_mix_pie = px.pie(df, values='no', names='type', color='type', color_discrete_map=color_discrete_map)
unit_mix_pie.show()

enter image description here

Then you can define unit_mix_bar as a plotly graph_object to add the bars individually uses traces, mapping their type to their color (I am borrowing from vestland's answer to a similar question):

import plotly.graph_objects as go

## add the bars one at a time
unit_mix_bar=go.Figure()
for t in df['type'].unique():
    dfp = df[df['type']==t]
    unit_mix_bar.add_traces(go.Bar(x=dfp['no'], y = dfp['type'], name=t,
                         marker_color=color_discrete_map[t]))
unit_mix_bar.show()

enter image description here

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

1 Comment

Thank you @Derek O, Apologise for not making myself clear. You are right, there are 2 subplots, one with 2 go.pie and second with 2 go.bar. I'm able to tweak around to change from go.pie and go.bar to px.pie and px.bar.

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.