I have a data frame df
, it has over 100 columns, I want to plot a stacked bar plot for these 100 columns, plotly.express
is very nice, I can just do this
import plotly.express as px
# df.columns = ['date', 'val1', 'val2', ..., 'val100', 'cost', 'numSales']
cols_to_be_stacked = df.columns[1:-2]
px.bar(df, x='time', y=cols_to_be_stacked)
But I want to have a subplots with (numRows=2, numCols=1), where the two rows share the x
axis,
fig = make_subplots(rows=2, cols=1)
## Q1: how should I do my_stacked_bar_plot for 100 columns?
fig.add_trace(my_stacked_bar_plot, row=1, col=1)
# Q2: I want to add time-cost plot to the y-axis on the right side, how
# should I write the line below?
# In matplotlib, i can just use ax.twinx().
fig.add_trace(go.Scatter(x=df['date'].values, y=df['cost'].values), row=1, col=1)
## now plot the time-numSales on 2nd row.
fig.add_trace(go.Scatter(x=df['date'].values, y=df['numSales'].values), row=2, col=1)
Can someone help me with Q1
and Q2
in the comments above? Or is there another way to achieve this other than using ๏ฝadd_trace๏ฝ?
Thanks!
EDIT (shorter description as below)
As a simple illustration, if I have a df
like this (here I have only 6 of val
columns instead of 100 val
columns
df = pd.DataFrame([[20240502, -2, 3, -3, 7, -9, 6, 6, 8],
[20240503, 4, -6, -5, 7, -3, -2, 12, 9]],
columns=["date", 'val1', 'val2', 'val3', 'val4',
'val5', 'val6', 'cost', 'numSales'])
date val1 val2 val3 val4 val5 val6 cost numSales
0 20240502 2 3 -3 -7 9 6 6 8
1 20240503 4 -6 5 7 -3 8 12 9
I want to a plot like below, (x-axis is shared for the top and bottom subplot)