✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

How can I update the legends value in Pie Chart?

Hi all,

I am practising building a Pie Chart in Plotly Express using Python.
So, this is the Pie Chart that I made;

This chart was build from a file with two columns called

  • gender with values of [0, 1, 2]
  • count_genders with values of [total_count_0, total_count_1, total_count_2]

I am planning to add some description to those values; for instance

  • 0 - female
  • 1 - male
  • 2 - undefined

This is where I am currently stuck. If I remember correctly changing the labels in legends relates to the data-ticks/or something along that line (at least in choropleth map).

Is there a way to do something similar in Pie Chart?

My current code for this graph:

gender_distribution = px.pie(users_genders,
                             values='count_genders',
                             names='gender',
                             color_discrete_map={'0': 'blue',
                                                 '1': 'red',
                                                 '2': 'green'},
                             title='Gender Distribution <br>'
                                   'between 2006-02-16 to 2014-02-20',
                             hole=0.35
                             )
gender_distribution.update_traces(textposition='outside',
                                  textinfo='percent+label',
                                  marker=dict(line=dict(color='#000000',
                                                        width=4)),
                                  pull=[0.05, 0, 0.03],
                                  opacity=0.9,
                                  # rotation=180
                                  )
gender_distribution.update_layout(legend=dict({'traceorder': 'normal'}
                                              # ticks='inside',
                                              # tickvals=[0, 1, 2],
                                              # ticktext=["0 - Female",
                                              #           "1 - Male",
                                              #           "2 - Undefined"],
                                              # dtick=3
                                              ),
                                  legend_title_text='User Genders'
                                  )
gender_distribution.show()

(I tried to use the ticks on the update_layout, but an error message was thrown at me. It was something about incorrect parameters I think.)

Many thanks for your time!