I hope someone had a similar problem and can help me.
At the moment I build a dashoard with Dash. Therefore I made some graphs with plotly and they look almost perfect. My last problem is that I cannot align the graph title with the y-axis title. Let me picture this for you:
____________________________
| tttttttt |
| gggggggggggggggg lll |
| y gggggggggggggggg lll |
| y gggggggggggggggg lll |
| y gggggggggggggggg lll |
| xxx |
|____________________________|
t is title, x/y is x/y-axis label, l is the legend and g is the graph
I want to get that t always starts at the same x-position like y.
Until now I tried the default xref='container'
and set x
to an specific number < 0 to hardcode the offset. This works until I use the same configuration for another graph where l is unequal 3 ālā long. Because of this, the graph gets moved to the left and my title alignment is crashed, because I hardcoded it. Sure, I could hardcode it for each graph, but at the end there will be over hundreds of graphs so this is no real solution I think about. Next I thought about using xref='paper'
and moving the title with a negative number for x
to the left because I know the space between graph and y-axis label and I also know the height of y. The problem here is that x is not allowed to be negative to move it to the left.
Hopefully someone can help me to solve this problem.
Here is a MWE where you can see that the y-axis label moves to the left.
MWE:
import plotly.graph_objects as go
import plotly.express as px
def set_layout(fig, title:str):
fig.update_layout(
title={'font_size':20, 'text': title, 'x':0.0075},
margin=dict(pad=3, t=80, l=100, r=100, b=80),
legend={'font_size':15, 'title_font_size':18},
showlegend=True)
fig.update_xaxes(title_text='this is a x axis label', title_font=dict(size=18), tickfont=dict(size=15))
fig.update_yaxes(title_text='this is a y axis label', title_font=dict(size=18), tickfont=dict(size=15))
return fig
if __name__ == '__main__':
fig = go.Figure()
fig.add_trace(go.Bar(name="short legend", x=["a", "b"], y=[2, 1]))
set_layout(fig, title='this is a title and is perfectly aligned with y axis label')
fig.show()
fig = go.Figure()
fig.add_trace(go.Bar(name="this is a pretty long legend that moves the graph to the left this is a pretty long legend that moves the graph to the left this is a pretty long legend that moves the graph to the left this is a pretty long legend that moves the graph to the left", x=["a", "b"], y=[2, 1]))
set_layout(fig, title='this is a title but its not more aligned because the legend got bigger')
fig.show()