You’re dealing with two different titles here.
If you define the title inside px.bar()
then this is your figure title.
If you define it inside .update_xaxes()
then it’s the title of the x-axis.
In the first code snippet, you defined a figure title as "Incremental Sales..."
but didn’t define any title for the x-axis, which by default will be set to x
since you didn’t pass a column name as x
in your px.bar() (otherwise x would have been replaced by the column name). The reason it appears at the top of the figure is because of showticklabels=False
, which prevents any x-axis label to be displayed below your x-axis:
fig = px.bar(df, x=[0,0], y="Values in euros", color = "Name", barmode='relative', template="simple_white",
text_auto=True, color_discrete_map={'AD2': '#636efa', 'AD1': '#ced1fd'}, title = "Figure Title")
fig.update_xaxes(showticklabels=False, ticks = "", title='X-axis Title')
fig.update_layout(coloraxis_showscale=False)
fig.add_hline(y=150, line_width= 3, line_color='red', opacity=1)
fig.add_hline(y=100, line_width= 3, line_color='black', opacity=1)
In the second code snippet, you don’t define a figure title but only a title for your x-axis, which here again will appear at the top of the figure because of showticklabels=False
. If you wish to hide the ticks and their labels but keep the title below, here’s a work around:
fig = px.bar(df, x=[0,0], y="Values in euros", color = "Name", barmode='relative', template="simple_white",
text_auto=True, color_discrete_map={'AD2': '#636efa', 'AD1': '#ced1fd'}, title = "Figure Title")
fig.update_xaxes(tickvals=[0], ticktext=[" "], ticks = "", title='X-axis Title')
fig.update_layout(coloraxis_showscale=False)
fig.add_hline(y=150, line_width= 3, line_color='red', opacity=1)
fig.add_hline(y=100, line_width= 3, line_color='black', opacity=1)