Display True/False to two charts per task

Hello, I want to display my data using plotly. Here is a example of my dataset:
id,expNumber,attempt,finished,successful,
1,1,1,true,false,
1,1,2,false,false,
1,1,3,true,true,
1,2,1,false,false,
1,2,2,true,false,
1,2,3,true,true,
1,4,1,false,false,
1,4,2,false,false,

What I want to do is to display the sum of true/false per expNumber in a bar chart. Here is a little mockup:

image

I use the following Code but it doesnt work (It is just a snippet). I just get the sum of all expNumber per True / False. Do you know how I can modify x and y axis to get a result like the mockup?

if "expNumber" in dff:
    return [
        dcc.Graph(id='bar-chart',
                  figure=px.bar(
                      data_frame=dff,
                      x="successful",
                      y="expNumber",
                      labels={"": ""}
                  ).update_layout(showlegend=False, xaxis={'categoryorder': 'total ascending'})
                  .update_traces(hovertemplate="<b>%{y}%</b><extra></extra>")
                  )

    ]

Hi, I think you should make a new column same with successful column and use it to create chart. Please check below code is what you want or not:

import pandas as pd
import numpy as np
import plotly.graph_objects as go

df = pd.DataFrame({'id':[1,1,1,1,1,1,1,1],
                   'expNumber':[1,1,1,2,2,2,4,4],
                   'attempt':[1,2,3,1,2,3,1,2],
                   'finished':['true','false','true','false','true','true','false','false'],
                   'successful':['false','false','true','false','false','true','false','false']})

df['successful_2'] = df['successful']

df3 = df.pivot_table(values='successful',
                     index=['expNumber','successful_2'],
                     aggfunc='count').reset_index()

fig_1 = go.Figure(data=[
go.Bar(name='Type', x=[tuple(df3['expNumber']), tuple(df3['successful_2'])],
               y=list(df3['successful'])),])
fig_1.update_traces(marker_color='lightseagreen')
fig_1.update_layout(margin=dict(l=0, r=0, t=0, b=0))
fig_1.show()

Here is what it shows:

1 Like

Thanks! It works for me but its a bit different to my code. This is a frame using Plotly go. My frame needs to be a dcc.graph because I need to change data on the base of a DashTable. Is it possible to use this proceeding for a dcc.graph?

Yep so instead of figure = px.bar you can you figure = go.Figure. Because I don’t have your full code so it’s just my suggestion.