Python: How to center the Title in Plotly [3 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# Python: How to center the Title in Plotly

To center the title in Plotly:

  1. Call the fig.update_layout() method.
  2. Set the title_x argument to 0.5.
  3. The title_x argument is a number between 0 and 1 and sets the x position of the title.
main.py
import plotly.express as px df = px.data.iris() fig = px.scatter( df, x="sepal_length", y="sepal_width", color="species", title="Example title here" ) fig.update_layout(title_text='Example title', title_x=0.5) fig.show()

center title in plotly

The code for this article is available on GitHub

The code sample uses the fig.update_layout() method to center the title.

If I remove the method call from the code sample, the title won't be centered.

main.py
import plotly.express as px df = px.data.iris() fig = px.scatter( df, x="sepal_length", y="sepal_width", color="species", title="Example title here" ) fig.show()

removing fig update layout method call

The code for this article is available on GitHub

The following line centers the plot's title.

main.py
fig.update_layout(title_text='Example title', title_x=0.5)

The title_text argument is a string that sets the plot's title.

The title_x argument is a number between or equal to 0 and 1.

The number sets the x position from 0 (left) to 1 (right).

We set the title_x argument to 0.5 to center the title but you can play around with the value depending on your needs.

# You can also pass a dictionary with the title key to fig.update_layout()

If you need to be more specific when centering the title, you can also pass a nested dictionary when calling fig.update_layout().

main.py
import plotly.express as px df = px.data.iris() fig = px.scatter( df, x="sepal_length", y="sepal_width", color="species", title="Example title here" ) fig.update_layout( { 'title': { 'text': 'Example title text', 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top', 'font': {'size': 20}, } } ) fig.show()
The code for this article is available on GitHub

Notice that the dictionary we passed to the method has a title key that stores a nested dict.

passing a dictionary with the title key to fig update layout

Since the outer dictionary already has the title key, notice that we didn't use the title_ prefix in the inner dict's keys.

For example, title_text becomes text and title_x becomes x.

main.py
fig.update_layout( { 'title': { 'text': 'Example title text', 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top', 'font': {'size': 20}, } } )

The xanchor argument can be set to one of "auto", "left", "center" and "right".

The argument sets the legend's horizontal position anchor.

In the example, it binds the x position to the center of the legend.

By default, the argument is set to "left".

The yanchor argument can be one of "auto", "top", "middle" and "bottom".

The font key is a dictionary that may have the color, family and size keys.

You can view all of the available keys of the title dictionary in this section of the plotly docs.

You can also style the Plotly Express title in different ways.

For example, you can wrap the title text in a <b> tag to bold it.

main.py
import plotly.express as px df = px.data.iris() fig = px.scatter( df, x="sepal_length", y="sepal_width", color="species", title="Example title here" ) fig.update_layout( { 'title': { 'text': '<b>Example title text</b>', 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top', 'font': {'size': 20, 'color': 'darkgreen'}, } } ) fig.show()
The code for this article is available on GitHub

I also set the color key under font to darkgreen.

style plotly express title

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.