🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

How to create a barchart where some bars are grouped and some are stacked

I want to create a barchart where the last bar is on top of one other bar. This is valuable for forecasts where you want to make clear that the bar on top is different, but in the same category.

I made an example code:

np.random.seed(1)
x = [0, 1, 2, 3, 4]
y1 = np.random.randint(1, 10, size=5)
y2 = np.random.randint(1, 8, size=5)
y22 = np.random.randint(1, 3, size=5)

fig = go.Figure()

fig1 = go.Bar(x=x, y=y1, name = 'revenue last year')
fig2 = go.Bar(x=x, y=y2, name = 'revenue this year')
fig22 = go.Bar(x=x, y=y22, name = 'revenue this year (forecast)')

fig.add_traces([
    fig1, 
    fig2,
    fig22
])

This results in

I want this (made with paint)

I managed to do this


But it wasn’t what I wanted. It is unclear what is the forecast and what part is not. But if it’s possible to change the top colour, I would be satisfied. But I have not found how to do that.