Search code examples
pythonplotlyplotly-python

How to assign different fonts and size to title and axis in plotly?


im trying to separately set the font/size for the axis and the title. Ex: For title: Font = Ariel and size = 12. For Axis: Font = Times New Roman and size = 20. Im using the following code to do it.

fig.update_layout(font=dict(family='Times New Roman',size=20,\
title=dict(font=dict(family='Ariel', size=12)))

When I do this, only the font change will take into effect and it will change everything, but title never does anything. Thank you and have a good day


Solution

  • There are examples of changing titles, axis labels, etc. in the official reference. Based on this example, I further changed the partial color and size of the title. This tip is referenced here.

    import plotly.express as px
    
    df = px.data.iris()
    fig = px.scatter(df, x="sepal_length", y="sepal_width", color="species",
                    title="Playing with <span style='font-size:22px;color:black;'>Fonts</span>")
    fig.update_layout(
        font_family="Courier New",
        font_color="blue",
        title_font_family="Times New Roman",
        title_font_color="red",
        legend_title_font_color="green"
    )
    fig.update_xaxes(title_font_family="Arial")
    fig.show()
    

    enter image description here