8.6 C
London
Friday, December 20, 2024
HomeSoftware TutorialsPythonHow to Adjust Number of Ticks in Seaborn Plots

How to Adjust Number of Ticks in Seaborn Plots

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to specify the positions and labels of axis ticks on seaborn plots:

#specify x-axis tick positions and labels
plt.xticks([1, 2, 3], ['A', 'B', 'C'])

#specify y-axis tick positions and labels
plt.yticks([4, 5, 6], ['D', 'E', 'F'])

The following examples show how to use this syntax in practice.

Example 1: Set Axis Tick Positions

The following code shows how to create a simple scatterplot using seaborn:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

#create DataFrame
df = pd.DataFrame({'var1': [25, 12, 15, 14, 19, 23, 25, 29],
                   'var2': [5, 7, 7, 9, 12, 9, 9, 4]})

#create scatterplot
sns.scatterplot(data=df, x='var1', y='var2')

By default, seaborn chooses an optimal number of ticks to display on both the x-axis and y-axis.

However, we can use the following code to specify the number of ticks and their exact positions on each axis:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

#create DataFrame
df = pd.DataFrame({'var1': [25, 12, 15, 14, 19, 23, 25, 29],
                   'var2': [5, 7, 7, 9, 12, 9, 9, 4]})

#create scatterplot
sns.scatterplot(data=df, x='var1', y='var2')

#specify positions of ticks on x-axis and y-axis
plt.xticks([15, 20, 25])
plt.yticks([4, 8, 12])

Example 2: Set Axis Tick Positions & Labels

The following code shows how to create a scatterplot and specify both the axis tick positions and the tick labels:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

#create DataFrame
df = pd.DataFrame({'var1': [25, 12, 15, 14, 19, 23, 25, 29],
                   'var2': [5, 7, 7, 9, 12, 9, 9, 4]})

#create scatterplot
sns.scatterplot(data=df, x='var1', y='var2')

#specify positions of ticks on x-axis and y-axis
plt.xticks([15, 20, 25], ['A', 'B', 'C'])
plt.yticks([4, 8, 12], ['Low', 'Medium', 'High'])

Note: Refer to this article to see how to change just the axis labels.

Additional Resources

The following tutorials explain how to perform other common functions in seaborn:

How to Adjust the Figure Size of a Seaborn Plot
How to Add a Title to Seaborn Plots
How to Save Seaborn Plot to a File

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories