How to combine the command displaying text with go.Layout, please? The command fig.add_annotation stop working when go.Layout() is present. go.Layout includes many features; thus, I would like to not change it.
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
## sample DataFrames
df1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
fig = px.scatter(df1, x='A', y='B')
fig.add_annotation(text="Absolutely-positioned annotation",
x=2.5, y=4.5, showarrow=False)
layout = go.Layout(
template = "plotly_white",
title="<b>O-C diagram</b>",
font_family="Trebuchet",
title_font_family="Trebuchet",
title_font_color="Navy",
xaxis_tickformat = "5.0",
yaxis_tickformat = ".1f",
xaxis2 = XAxis(
overlaying='x',
side='top',
),
legend=dict(
yanchor="top",
y=0.99,
xanchor="left",
x=0.83,
font=dict(
family="Trebuchet",
size=20,
color="black"
),
bgcolor="LightGray",
bordercolor="Black",
borderwidth=0
),
xaxis_title=r'<i>T</i>',
yaxis_title=r'O',
title_x=0.5,
font=dict(
family="Trebuchet",
size=26,
color="Black"
),
)
fig.layout = layout
fig.show()
The problem here isn't that you're using:
layout = go.Layout(
template = "plotly_white",
)
But rather that...
fig.layout = layout
...overrides every layout attribute other than template
.
If you instead use:
fig.update_layout(template = "plotly_white")
Then you'll get:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
## sample DataFrames
df1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
fig = px.scatter(df1, x='A', y='B')
fig.add_annotation(text="Absolutely-positioned annotation",
x=2.5, y=4.5, showarrow=False)
# layout = go.Layout(
# template = "plotly_white",
# )
# fig.layout = layout
fig.update_layout(template = "plotly_white")
fig.show()
If you for some reason need to stick with your original setup, then just change the order you're assigning the template and the annotations to your figure:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
## sample DataFrames
df1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
fig = px.scatter(df1, x='A', y='B')
layout = go.Layout(
template = "plotly_white",
)
fig.layout = layout
fig.add_annotation(text="Absolutely-positioned annotation",
x=2.5, y=4.5, showarrow=False)
fig.show()