Lesson

Custom axis and tick formatting

Learn Custom axis and tick formatting in SQLPad's Data Science in Action course with practical examples and guided lessons.

In this lesson, we will learn how to customize axis and tick formatting in Plotly.

Table of Contents

  1. Customizing Axis Labels
  2. Axis Range
  3. Tick Mode and Tick Values
  4. Tick Formatting
  5. Axis Type and Scale
  6. Grid Lines and Zero Lines

1. Customizing Axis Labels

Axis labels help us better understand the data represented on the chart. Let's customize the axis labels for a scatter plot.

import plotly.express as px

data = px.data.iris()

fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species')

fig.update_xaxes(title_text='Sepal Width (cm)')
fig.update_yaxes(title_text='Sepal Length (cm)')

fig.show()

2. Axis Range

We can set the range of the axis to focus on a specific area of the chart.

import plotly.express as px

data = px.data.iris()

fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species')

fig.update_xaxes(title_text='Sepal Width (cm)', range=[2, 4.5])
fig.update_yaxes(title_text='Sepal Length (cm)', range=[4, 8])

fig.show()

3. Tick Mode and Tick Values

We can control the tick mode and tick values displayed on the axis. There are two modes: 'auto' and 'array'. The 'array' mode allows us to set custom tick values.

import plotly.express as px

data = px.data.iris()

fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species')

fig.update_xaxes(title_text='Sepal Width (cm)', tickmode='array', tickvals=[2, 3, 4, 5])
fig.update_yaxes(title_text='Sepal Length (cm)', tickmode='array', tickvals=[4, 5, 6, 7, 8])

fig.show()

4. Tick Formatting

We can format the tick labels using the tickformat parameter.

import plotly.express as px

data = px.data.gapminder()

fig = px.scatter(data, x='gdpPercap', y='lifeExp', color='continent', size='pop', size_max=60, hover_name='country',
                 facet_col='continent', log_x=False, animation_frame='year', animation_group='country',
                 height=400, title='Life Expectancy vs GDP per Capita')

fig.update_xaxes(tickprefix='$', tickformat=',.0f')
fig.update_yaxes(tickformat='.1f')

fig.show()

5. Axis Type and Scale

We can change the axis type and scale to better represent the data. For example, we can change to a logarithmic scale.

import plotly.express as px

data = px.data.gapminder()

fig = px.scatter(data, x='gdpPercap', y='lifeExp', color='continent', size='pop', size_max=60, hover_name='country',
                 facet_col='continent', animation_frame='year', animation_group='country',
                 height=400, title='Life Expectancy vs GDP per Capita')

fig.update_xaxes(type='log')
fig.update_yaxes(tickformat='.1f')

fig.show()

6. Grid Lines and Zero Lines

We can customize the appearance of grid lines and zero lines on the chart.

import plotly.express as px

data = px.data.iris()

fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species')

fig.update_xaxes(title_text='Sepal Width (cm)', showgrid=True, gridwidth=1, gridcolor='LightGray', zeroline=True, zerolinewidth=2, zerolinecolor='Black')
fig.update_yaxes(title_text='Sepal Length (cm)', showgrid=True, gridwidth=1, gridcolor='LightGray', zeroline=True, zerolinewidth=2, zerolinecolor='Black')

fig.show()

In this lesson, we have covered custom axis and tick formatting in Plotly. You can now customize your charts to better represent your data and improve readability.

Exercises

1. Custom Axis and Tick Formatting

Instruction

In this exercise, you will create a scatter plot using the Iris dataset and customize the axis labels, tick mode, tick values, and grid lines. Follow these steps:

  1. Import the plotly.express library as px.
  2. Load the Iris dataset using px.data.iris() and store it in a variable called data.
  3. Create a scatter plot using px.scatter() with data, x='sepal_width', y='sepal_length', and color='species'. Store the plot in a variable called fig.
  4. Update the x-axis label to 'Sepal Width (cm)' and set the tick mode to 'array' with tick values [2, 3, 4, 5].
  5. Update the y-axis label to 'Sepal Length (cm)' and set the tick mode to 'array' with tick values [4, 5, 6, 7, 8].
  6. Customize the grid lines and zero lines for both x and y axes with the following settings: showgrid=True, gridwidth=1, gridcolor='LightGray', zeroline=True, zerolinewidth=2, and zerolinecolor='Black'.
  7. Display the plot using fig.show().

My Solution

# Your solution goes here

Hint

Start by importing the plotly.express library and loading the Iris dataset. Then, create a scatter plot using px.scatter(). Update the x and y axes using fig.update_xaxes() and fig.update_yaxes() with the given parameters. Finally, display the plot using fig.show().

Solution

import plotly.express as px

data = px.data.iris()

fig = px.scatter(data, x='sepal_width', y='sepal_length', color='species')

fig.update_xaxes(title_text='Sepal Width (cm)', tickmode='array', tickvals=[2, 3, 4, 5], showgrid=True, gridwidth=1, gridcolor='LightGray', zeroline=True, zerolinewidth=2, zerolinecolor='Black')
fig.update_yaxes(title_text='Sepal Length (cm)', tickmode='array', tickvals=[4, 5, 6, 7, 8], showgrid=True, gridwidth=1, gridcolor='LightGray', zeroline=True, zerolinewidth=2, zerolinecolor='Black')

fig.show()