Hello,
The following code produces a stacked bar-chart. However I am unable to increase the width of the bars produced. I have used the width parameter as discussed in documentation but it isnโt helping.
The code is as follows:
#python
import datetime
import plotly.graph_objects as go
from numpy import array
def plot_barchart(title_p: str, x_label: str, y_label: str):
barchart_fig_go = go.Figure(layout=go.Layout(plot_bgcolor='#FFFFFF'))
barchart_fig_go.add_trace(
go.Bar(
name='INDEX1',
x=array([datetime.datetime(2022, 1, 31, 0, 0),
datetime.datetime(2022, 2, 28, 0, 0),
datetime.datetime(2022, 3, 31, 0, 0),
datetime.datetime(2022, 4, 29, 0, 0),
datetime.datetime(2022, 5, 31, 0, 0),
datetime.datetime(2022, 6, 30, 0, 0),
datetime.datetime(2022, 7, 29, 0, 0),
datetime.datetime(2022, 8, 31, 0, 0),
datetime.datetime(2022, 9, 30, 0, 0),
datetime.datetime(2022, 10, 31, 0, 0),
datetime.datetime(2022, 11, 30, 0, 0),
datetime.datetime(2022, 12, 30, 0, 0),
datetime.datetime(2023, 1, 31, 0, 0),
datetime.datetime(2023, 2, 28, 0, 0),
datetime.datetime(2023, 3, 31, 0, 0),
datetime.datetime(2023, 4, 28, 0, 0),
datetime.datetime(2023, 5, 31, 0, 0),
datetime.datetime(2023, 6, 2, 0, 0)], dtype=object),
y=array([-0.00348633, -0.00259967, 0.02014313, -0.00132585, -0.00307056,
0.00600349, -0.00106385, -0.00131315, -0.01125965, -0.00553509,
0.01206035, -0.00654606, 0.01196162, 0.00026996, 0.00474922,
0.00020395, 0.00169865, 0.00119708]),
width=[0.8,0.8]
)
)
barchart_fig_go.add_trace(
go.Bar(
name='INDEX2',
x=array([datetime.datetime(2022, 1, 31, 0, 0),
datetime.datetime(2022, 2, 28, 0, 0),
datetime.datetime(2022, 3, 31, 0, 0),
datetime.datetime(2022, 4, 29, 0, 0),
datetime.datetime(2022, 5, 31, 0, 0),
datetime.datetime(2022, 6, 30, 0, 0),
datetime.datetime(2022, 7, 29, 0, 0),
datetime.datetime(2022, 8, 31, 0, 0),
datetime.datetime(2022, 9, 30, 0, 0),
datetime.datetime(2022, 10, 31, 0, 0),
datetime.datetime(2022, 11, 30, 0, 0),
datetime.datetime(2022, 12, 30, 0, 0),
datetime.datetime(2023, 1, 31, 0, 0),
datetime.datetime(2023, 2, 28, 0, 0),
datetime.datetime(2023, 3, 31, 0, 0),
datetime.datetime(2023, 4, 28, 0, 0),
datetime.datetime(2023, 5, 31, 0, 0),
datetime.datetime(2023, 6, 2, 0, 0)], dtype=object),
y=array([1.21058407e-05, 1.27078335e-03, -1.91812714e-03, 1.00472469e-02,
3.14907763e-04, -1.98958319e-03, -8.61364898e-03, 1.54027556e-03,
-5.37822304e-03, -1.18758409e-02, -1.71503169e-04, 5.94205473e-03,
5.48899570e-03, 1.35409234e-02, -3.16090870e-03, -4.06147230e-03,
3.71189220e-03, 1.23621180e-03]),
width=[0.8,0.8]
)
)
barchart_fig_go.update_layout(
xaxis_tickangle=-45,
xaxis=dict(
title=x_label
),
yaxis=dict(
title=y_label
),
title=title_p,
barmode='stack'
)
barchart_fig_go.update_yaxes(showline=True, linewidth=2, gridcolor='#CCCCCC')
return barchart_fig_go
monthly_bar = plot_barchart(
title_p="SAMPLE CHART",
x_label='Date',
y_label='')
monthly_bar.write_image('SAMPLE' + '_' + '_ma.png',
width=1200,
height=1000, engine='kaleido')