Hi,
I am trying to work on Pie Subplots in a Plotly using python. I have a dataset where I need to plot multiple Pie Chart based on the bins which I created for the score range from a test. I am finding it very difficult to plot it in Plotly.
Below is the DataFrame which I could come up by working on the dataset using pandas.

scores,0-2 score,2-5 score,5-10 score,10-20 score
Test,
tutorial_101,942,136,1,0
tutorial_201,524,21,0,0
tutorial_301,540,11,0,0
tutorial_401,541,15,2,0
tutorial_501,58,655,1,1
tutorial_601,514,18,0,0
tutorial_701,520,20,0,0
tutorial_801,0,180,3,0
tutorial_901,137,47,0,0
tutorial_1001,142,43,1,0
tutorial_1101,143,44,2,0
tutorial_1201,4,171,3,0
I could not figure out a way to plot the above Pivoted DataFrame in Plotly Pie Subplots.
Can someone please help?
Thanks in advance
Hi @rohini_l
Are you trying to do something like this? This pie is only for 0-2 scores. The biggest pie slice of 23.2% belongs to 0-2 score=942
If not, can you please explain how you want your pie charts to look like.
Adam
Thanks Adam, Actually I am looking Pie Chart for each Tutorial with the scores being the Values [โ0-2 scoresโ,โ2-5 scoresโ, โ5-10 scoresโ, โ10-20 scoresโ โฆ ]
Something like below ->
There might be a faster way of doing this. Iโm not sure. But one way I found is melting your data with pandas and creating separate dataframes for each test/tutorial. I did it like this:
import pandas as pd
import plotly.io as pio
import plotly.graph_objects as go
from plotly.subplots import make_subplots
df = pd.read_csv("testing4.csv")
print(df[:5])
df = df.melt(id_vars=["test"], var_name="scores")
print(df[:5])
df = df.groupby(['test'])
dff = [df.get_group(x) for x in df.groups]
print(dff[0])
print(dff[1])
# Create subplots:
fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])
# Create subplots: for 12 tutorials you can do
# fig = make_subplots(rows=4, cols=3, specs=[[{'type':'domain'}, {'type':'domain'},{'type':'domain'},{'type':'domain'}
# {'type':'domain'},{'type':'domain'},{'type':'domain'},{'type':'domain'},
# {'type':'domain'},{'type':'domain'},{'type':'domain'},{'type':'domain'}]])
fig.add_trace(go.Pie(labels=dff[0]['scores'], values=dff[0]['value'], name="tutorial_101"),
1, 1)
fig.add_trace(go.Pie(labels=dff[1]['scores'], values=dff[1]['value'], name="tutorial_102"),
1, 2)
pio.show(fig)
How does this work for you.
Did you modify the original data frame I shared. For me it throws me an error:
KeyError: โThe following โid_varsโ are not present in the DataFrame: [โTestโ]โ
Thanks
Sorry. I used these columns names in side testing4.csv:
test |
0-2 score |
2-5 score |
5-10 score |
10-20 score |
tutorial_101 |
942 |
136 |
1 |
0 |
tutorial_201 |
524 |
21 |
0 |
0 |