Title alignment in Plotly

24 Jan, 2022
In this article, we are going to discuss title assignment and alignment using plotly module.
In order to align titles in visualization using plotly module, we are going to use the update_layout() method. 
Syntax:
plotly.graph_objects.Figure.update_layout(title_text, title_x, title_y)
Parameters:
title: Accepts string value as the title of the visualization.
title_x: This parameter is used to align the title in a horizontal motion and accepts a value from 0 to 1.
title_y: This parameter is used to align the title in a vertical motion and accepts a value from 0 to 1.
Let’s implement the method on the below examples:
Example 1
Python3
# import all required librariesimport numpy as npimport plotlyimport plotly.graph_objects as goimport plotly.offline as pyofrom plotly.offline import init_notebook_mode init_notebook_mode(connected=True) # generating 150 random integers# from 1 to 50x = np.random.randint(low=1, high=50, size=150)*0.1 # generating 150 random integers# from 1 to 50y = np.random.randint(low=1, high=50, size=150)*0.1 # plotting scatter plotfig = go.Figure(data=go.Scatter(x=x, y=y, mode='markers')) # title alignmentfig.update_layout(title_text='Dots') fig.show()
Output:
Example 2
Python3
# import all required librariesimport numpy as npimport plotlyimport plotly.graph_objects as goimport plotly.offline as pyofrom plotly.offline import init_notebook_mode init_notebook_mode(connected = True) # different individual parts in# total chartcountries=['India', 'canada',        'Australia','Brazil',        'Mexico','Russia',        'Germany','Switzerland',        'Texas'] # values corresponding to each# individual country present in# countriesvalues = [4500, 2500, 1053, 500,        3200, 1500, 1253, 600, 3500] # plotting pie chartfig = go.Figure(data=[go.Pie(labels=countries,                    values=values)]) # title alignmentfig.update_layout(title_text='Pie',title_y=0.5) fig.show()
Output:
With title_y=0.5, the title should be in center.
When title_y = 0.1
When title_y = 1
Example 3
Python3
# import required librariesimport dashimport dash_core_components as dccimport dash_html_components as htmlimport plotly.express as pximport pandas as pd external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] app = dash.Dash(__name__, external_stylesheets=external_stylesheets) # assume you have a "long-form" data framedf = pd.DataFrame({    "Year": ["2015", "2016", "2017", "2015", "2016", "2017"],    "Revenue": [4, 1, 2, 2, 4, 5],    "Company": ["ABC Pvt. Ltd.", "ABC Pvt. Ltd.", "ABC Pvt. Ltd.", "XYZ Pvt. Ltd.", "XYZ Pvt. Ltd.", "XYZ Pvt. Ltd."]}) # depict visualizationfig = px.bar(df, x="Year", y="Revenue", color="Company", barmode="group") app.layout = html.Div(children=[     dcc.Graph(        id='example-graph',        figure=fig    )]) # align titlefig.update_layout(title_text='Revenue', title_x=0.5) if __name__ == '__main__':    app.run_server(debug=True)
  
Output:
 
With title_x=0.5, the title should be in center.
 
 
When title_x = 0.3
 
 
When title_x = 1
 
 
manikarora059
sagar0719kumar
Picked
Python-Plotly
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to iterate through Excel rows in Python?
Deque in Python
Defaultdict in Python
Queue in Python
Rotate axis tick labels in Seaborn and Matplotlib
Check if element exists in list in Python
Python Classes and Objects
Bar Plot in Matplotlib
Python OOPs Concepts
How To Convert Python Dictionary To JSON?