How to center title in Plotly

In Plotly, you can center the title of a plot by setting the title.x attribute to 0.5 and the title.xref attribute to “paper”.

Here’s an example of how you can center the title of a plot in Plotly:


import plotly.express as px

# Create a sample plot
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="smoker")

# Center the title
fig.update_layout(
    title=dict(
        text="Tips Dataset",
        font=dict(size=24),
        x=0.5,
        xref="paper"
    )
)

# Show the plot
fig.show()

In this example, we use Plotly Express to create a scatter plot of the tips dataset, and then we update the plot layout to center the title. The text attribute sets the title text, font sets the font size, x sets the title x position, and xref sets the x reference. Finally, we show the plot using the show method of the figure object.

See also  Adding Traces to Plotly Charts in Python