Код: Выделить всё
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)
Код: Выделить всё
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)
Спасибо!
Изменить (более короткое описание, как показано ниже)
В качестве простой иллюстрации: если у меня есть такой df (здесь у меня только 6 столбцов val вместо 100 столбцов val
Код: Выделить всё
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

Подробнее здесь: https://stackoverflow.com/questions/784 ... -in-plotly