Title and Axes
Learn how to format titles and axes in Plotly figures.
Customizing titles and axes
In this course, we will use the fig.update_layout() method to adjust the aesthetic element of our plot(s). We can customize the title, x-axis, y-axis, and more. This becomes exceptionally powerful when addressing client demands and business needs.
We will start by using a customer churn dataset.
Updating the title
To adjust only the title text, we can simply pass a string to the title keyword argument in the fig.update_layout() method. The same applies to the x and y axes, where we can simply pass a string to the xaxis_title and yaxis_title keyword arguments.
In the plot below, we have a box plot that investigates the spread of the peoples’ ages in each country. Hence it is suitable to give this graph the title of Ages by Country. We’ll also label the x-axis as Country and the y-axis as Age. Pay close attention to the code snippet below:
fig.update_layout(title='Ages by Country', xaxis_title='Country', yaxis_title='Age')
The full implementation is given below:
# Import librariesimport plotly.express as pximport plotly.graph_objects as goimport pandas as pdimport numpy as np# Import datasetchurn = pd.read_csv("/usr/local/csvfiles/churn.csv")# Create our boxplot, with country on the x axis and ages on the y axistrace = go.Box(x=churn['Geography'], y=churn['Age'])# Create figure objectfig = go.Figure(data=[trace])# Make small refinements to layoutfig.update_layout(title='Ages by Country',xaxis_title='Country',yaxis_title='Age')# Show figure to the screenfig.show()
Title settings: Deep dive
Here we will go into more detail about how we can intricately style the title of a graph:
If we want to style a title to its finest details, we can use the Title class from plotly.graph_objects.layout or go.layout, considering how we have imported the graph_objects module. This can then be passed to fig.update_layout() as the title keyword argument. Here we use a box plot, but since a title is generally present irrespective of the graph type, we can use this for all plots we decide to be suitable for our visualization task. We will keep the x-axis and y-axis settings very simple, as demonstrated in the previous plot.
In the plot below, we place a variety of keyword arguments to ...