Below is the working sample code that populates 3 subplots. I am trying to show 1st subplot as ‘group’(row=1,col=1) and the remaining two subplots as ‘stack’. I have also selected the ‘barmode’ for each subplot. But my final figure has all ‘stack’ subplots. I have referred few forum and they suggested using ‘offset’ for each subplot but even that parameter is not working out. Please suggest. Note: In real-time, all subplots are based on different dataframes but I took only one dataframe for this post.
import pandas as pd
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
#creating my dataframe
s = 200
df = pd.DataFrame({"Country": np.random.choice(["USA America", "JPY one two", "MEX", "IND",
"AUS"], s),
"economy_cat": np.random.choice(["developing","develop"], s),
"gdp": np.random.randint(5, 75, s),
})
#filter dataframe for one country
df = df[df.Country=='USA America']
df_pivot = df.pivot_table(index='Country',columns='economy_cat',values='gdp',aggfunc='sum')
df_pivot.reset_index(inplace=True)
print(df_pivot)
fig = make_subplots(rows=2, cols=4)
# creation of subplots 1
fig.add_trace(
go.Bar(
x=df_pivot["Country"],
y=df_pivot["develop"],
marker_color = '#1f77b4',
name='develop'
),
row=1,
col=1,
)
fig.add_trace(
go.Bar(
x=df_pivot["Country"],
y=df_pivot["developing"],
marker_color = "rgba(255, 0, 0, 0.6)",
name='developing'
),
row=1,
col=1,
)
fig.update_layout(barmode = 'group')
# creation of subplots 2
fig.add_trace(
go.Bar(
x=df_pivot["Country"],
y=df_pivot["develop"],
marker_color = '#1f77b4',
name='develop'
),
row=1,
col=2,
)
fig.add_trace(
go.Bar(
x=df_pivot["Country"],
y=df_pivot["developing"],
marker_color = "rgba(255, 0, 0, 0.6)",
name='developing'
),
row=1,
col=2,
)
fig.update_layout(barmode = 'stack')
# creation of subplots 3
fig.add_trace(
go.Bar(
x=df_pivot["Country"],
y=df_pivot["develop"],
marker_color = '#1f77b4',
name='develop'
),
row=2,
col=1,
)
fig.add_trace(
go.Bar(
x=df_pivot["Country"],
y=df_pivot["developing"],
marker_color = "rgba(255, 0, 0, 0.6)",
name='developing'
),
row=2,
col=1,
)
fig.update_layout(barmode = 'stack')
fig.update_layout(
font_family="Droid Sans")