0

A radiation dose simulation gave the [following organ doses:][1]

I would like to use Python - Plotly to make a grouped bar chart with the five highest organ dose values from each age group columns '0', '1', '5', '10', and '15'.
The x-axis should show those five organs from each age column that gets the highest dose.

I have started with finding the five largest for the '0' age group and make a bar graph:

 import pandas as pd
 import plotl.express as px

 df = pd.read_csv('data_org_dos.csv')
 data = df.set_index('Organ')['0'].nlargest(5)
 fig = px.bar(data)
 fig.show()

How do I make a grouped bar graph with all the ages '0', '1', '5', '10' and '15' presented in the same graph?

Following @Timeless answer I could change the code to produce the grouped bar chart I aimed for:

import pandas as pd
import plotly.graph_objs as go

df = pd.read_csv("data_org_dos.csv")
labels = df.columns.map(lambda x: "Age: "+ x)[1:]

fig = go.Figure() 
 
for idx, age in enumerate(labels):
    data = df.set_index('Organ')[age.split()[-1]].nlargest(5).reset_index()
    fig.add_trace(go.Bar(x=data['Organ'], y=data[age.split()[-1]], name=age))

    
fig.update_xaxes(title_text='Organ')
fig.update_yaxes(title_text='E [mSv]')   
fig.update_layout(
    font=dict(
        size=18
    )
)

fig.show()
[ see image link][2]


  [1]: https://www.dropbox.com/s/qux9v9ubp5cskl6/data_org_dos.csv?dl=0
  [2]: https://i.stack.imgur.com/iiEAt.png
ptrber
  • 5
  • 2

1 Answers1

0

You need a combo of nlargest and go.Bar :

import pandas as pd
import plotly.subplots as sp
import plotly.graph_objs as go

df = pd.read_csv("Downloads/data_org_dos.csv")

labels = df.columns.map(lambda x: "Age: "+ x)[1:]
fig = sp.make_subplots(rows=len(labels), cols=1)

for idx, age in enumerate(labels):
    data = df.set_index('Organ')[age.split()[-1]].nlargest(5).reset_index()
    fig.add_trace(go.Bar(x=data['Organ'], y=data[age.split()[-1]], name=age), row=idx+1, col=1)
    
fig.update_layout(title="StackOverflow: OrganDoses", barmode="group")

fig.show()

Output :

enter image description here

Timeless
  • 22,580
  • 4
  • 12
  • 30
  • Thank you @Timeless! Nearly there, the graph should be a grouped bar graph in one plot. X-axis with five age groups. Each group has five organ dose bars that points up from the X-axis. Y-axis with dose levels as is now but one axis, no subplots. – ptrber Feb 03 '23 at 00:42