Search code examples
pythonplotly

How to vertically position/align yaxis title at bottom in plotly express


I would like to create a plotly express line chart with a yaxis title that is positioned at the bottom of the axis instead of the center. This is what I have tried:

import pandas as pd
from plotly import express as px

df = pd.DataFrame({'x': range(0,10)})
fig = px.line(df).update_layout(yaxis_anchor='free', yaxis_position=0)

This is what I get:

enter image description here

This is the desired output:

enter image description here


Solution

  • As far as I know, this location of the axes titles can't be changed. The argument yaxis_position=0 won't work because that changes the x coordinate of the yaxis in the plotting space.

    However one possible workaround would be to set the yaxis title to an empty string, then place the text down as an annotation at the correct location and angle.

    For example:

    fig = px.line(df).update_yaxes(title='')
    fig.add_annotation(x=-0.05, y=0, xref="paper", yref="y", showarrow=False, text="value", font=dict(size=14), textangle=-90)
    

    enter image description here