I am trying to replicate a stacked bar graph of a 1d data(series), something like this, stacked bar graph using plotly high level using plotly low level interface. I have used high level interface to get the above output. Below is the code I used,
import plotly.express as px
fig = px.bar(df_monthly_earnings.T, width =500,height= 500)
fig.show()
I want the exact output from a low level interface so, I tried this,
import plotly.graph_objects as go
fig = go.Figure(
data=[go.Bar(y=df_monthly_earnings.Earnings)]
)
fig.update_layout(width=500,height=500)
fig.show()
but the output is not as I expected. How to get the desired output using low level interface? I want the earnings to be stacked just like the desired one. I have tried many different ways but nothing is working.
df_monthly_earnings contains,
+-------+---------------+
| Month | Earnings |
+-------+---------------+
| 1 | 4.488701e+07 |
| 2 | 3.707876e+07 |
| 3 | 6.888739e+07 |
| 4 | 7.085714e+07 |
| 5 | 7.376993e+07 |
| 6 | 8.048304e+07 |
| 7 | 1.170489e+08 |
| 8 | 2.191993e+08 |
| 9 | 7.833628e+07 |
| 10 | 1.359896e+08 |
| 11 | 1.081981e+08 |
| 12 | 1.070073e+08 |
+-------+---------------+
This is a typical example of how easy it was to obtain a graph in Express, but it takes a lot of work to make it into a graph object. I need to get the data per pile and deal with it in a loop process in order to display the legend. Each pile also requires a color setting. Of the 24 standard colors available, 12 colors are used for color coding. I prepared 12 colors because the default colors would result in a repeat of 10 colors. The order of the legend is changed. I also added the title of the axis, etc.
import plotly.graph_objects as go
import plotly.express as px
colors = px.colors.qualitative.Light24
fig = go.Figure()
for i,row in df_monthly_earnings.iterrows():
fig.add_trace(go.Bar(x=['Earnings'], y=[row['Earnings']], marker=dict(color=colors[i]), name=str(row['Month'].astype(int))))
fig.update_layout(width=500,
height=500,
barmode='stack',
xaxis=dict(title_text='index'),
yaxis=dict(title_text='value'),
legend_title='Month',
legend_traceorder="normal",
)
fig.show()