I am using the annotation system in Plotly to annotate the axis of a graph. I am specifying coordinates using the "paper" setting in plotly. However, I have noticed that paper coordinates are not always uniform across a figure, at least when arrows are turned off. Does anyone know why this might be happening? Here I provide code to show you:
def test():
fig = go.Figure()
ylabels = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
annotations = []
for i, label in enumerate(ylabels):
print(label/100)
annotations.append(
dict(
yref = 'paper',
y = label/100, x = 0,
text = ylabels[1],
showarrow = False,
yshift = 0,
)
)
fig.update_layout(
annotations = annotations,
yaxis = dict(
showticklabels = False,
tickvals = ylabels,
range = [0, 100]
),
xaxis = dict(
showticklabels = False
)
)
fig.show()
Non-uniform spacing when arrows are off
For some reason, this has to do with turning off the arrows. When the arrows are on, they are evenly spaced across the x-axis.
The issue seems to be resolved by setting yanchor
equal to either top
, middle
, or bottom
, see the Plotly documentation.
import plotly.graph_objects as go
fig = go.Figure()
ylabels = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
annotations = []
for i, label in enumerate(ylabels):
annotations.append(
dict(
yref='paper',
y=label / 100,
x=0,
text=ylabels[1],
showarrow=False,
yshift=0,
yanchor='middle' # default is 'auto'
)
)
fig.update_layout(
annotations=annotations,
yaxis=dict(
showticklabels=False,
tickvals=ylabels,
range=[0, 100]
),
xaxis=dict(
showticklabels=False
)
)
fig.show()