Learn how to use Dash Bio for next-gen sequencing & quality control. 🧬 Access the recorded webinar.

Pie Chart Subplots almost working as i want them but

Styling my subplots - “I am so close I can smell the roses”

Hi All,

This is my first time trying to get subplots working and of course, I want pie charts as my subplots so goodbye Plotly Express hello Graphic Objects.

I am pulling in data from a couple of excel sheets into pandas and I can create standard Pie charts in px fine

This has the information I require and a legend that is displayed as I like it. (I would love to know how to get rid of the percentages on this pie though)

When I create the subplots using go I get the following

I have three issues with this that I cant figure out. After reading copious amounts of posts I’m still baffled.

My three issues are

  1. How do I get the year to display under the chart? I tried annotations but that just borked on me?
  2. I would like physical number instead of percentages on the subplot?
  3. How do I not chart items that have zero value?

I am guessing the legend will sort itself out when I get data selection correct?

The dataframes df15 and df16 contain the following (I am just showing df15 but 16 is similar, just different values)

business_unit
Company Secretariat      0
Data Privacy             1
Financial Risk           0
GmbH                     3
IT Development           0
Information Security    23

below is my code I use to create my subplots.

y2020group = y2020raca.groupby('business_unit')
y2021group = y2021raca.groupby('business_unit')
df15 = y2020group.apply(lambda x: x['action_id'].count())
df16 = y2021group.apply(lambda x: x['action_id'].count())

fig = make_subplots(
    rows=1, cols=2,
    specs=[[{"type": "domain"}, {"type": "domain"}]],
    subplot_titles=("<b>2020<b>","<b>2021<b>")
)

fig.add_trace(go.Pie(values = df15),
              row=1, col=1)

fig.add_trace(go.Pie(values = df16),
              row=1, col=2)

fig.update_layout(title_text="<b>Number of Action Items by Business Unit<b>",
                  height=600,
                  showlegend=True
                  )

fig.show()

Thanks in advance for your help.

Tom

OK, I have worked out two of the three so far:

2. I would like a physical number instead of percentages on the subplot?

Answer. fig.update_traces (textinfo=“label+value’”) but I have decided to keep percentages so it is actually

fig.update_traces (textinfo=“label+value+percent”)

3. How do I not chart items that have zero value?
This was a bit trickier to track down but I found the answer here - Hiding % labels in px.pie chart python - #2 by nirvikalpa

I added the following to my fig.update_layout;

              uniformtext_minsize=12,
              uniformtext_mode='hide'

and I had to add

                textposition='inside'

to my fig.update_traces entry

Onwards and upwards. Now to find out how to get number 1 dealt with and work out why my legend is showing the position in the dataframe and not the label