Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

How to add stacked bar plot in a subplot in Plotly?

$
0
0

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 = ['time', '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['time'].values, y=df['cost'].values), row=1, col=1)## now plot the time-numSales on 2nd row.fig.add_trace(go.Scatter(x=df['time'].values, y=df['numSales'].values), row=2, col=1)

Can someone help me with Q1 and Q2 in the comments above?Thanks!


Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>