Creating a histogram with "segments"

Welcome to the community, @Ricardo.

Are you referring to something like this?

import pandas as pd
import plotly.express as px

df = pd.DataFrame({'month': ['January', 'February', 'March'], 'engineer': [10,9,6], 'doctor': [2,1,7], 'lawyer': [2,4,3]})
fig = px.bar(df, x='month', y=df.columns[1:], barmode='stack')
fig.show()

newplot

This is a bar chart with the mode set to stack

Instead of stack there are other options like group. Applying this to your example, you could calculate the sum separately and add it as bar to the chart:

import pandas as pd
import plotly.express as px

df = pd.DataFrame({'month': ['January', 'February', 'March'], 'engineer': [10,9,6], 'doctor': [2,1,7], 'lawyer': [2,4,3]})
df['total'] = df[df.columns[1:]].sum(axis = 1)

fig = px.bar(df, x='month', y=df.columns[1:], barmode='group')
fig.show()

newplot (2)

1 Like