How do I use a log scale in Seaborn plots? 2

How do I use a log scale in Seaborn plots?

Using a log scale in Seaborn plots allows for better visualization of data that has a wide range of values. This is achieved by transforming the data into a logarithmic scale, which compresses the larger values and expands the smaller values. To use a log scale in Seaborn plots, the user can specify the scale parameter as ‘log’ in the desired plot function. This will result in the x and/or y axis being displayed in a logarithmic scale. This feature is particularly useful when dealing with data that has a large range of values, as it helps to display the data in a more visually appealing and comprehensive manner.

Use a Log Scale in Seaborn Plots


You can use the plt.xscale() and plt.yscale() functions to use a log scale for the x-axis and y-axis, respectively, in a seaborn plot:

import matplotlib.pyplotas plt
import seaborn as sns

#create scatterplot with log scale on both axes
sns.scatterplot(data=df, x='x', y='y')
plt.xscale('log')
plt.yscale('log')

The following example shows how to use these functions in practice.

Example: Use Log Scale in Seaborn Plot

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'x': [2, 5, 6, 7, 9, 13, 14, 16, 18],
                   'y': [200, 1700, 2300, 2500, 2800, 2900, 3400, 3900, 11000]})

#view DataFrame
print(df)

    x      y
0   2    200
1   5   1700
2   6   2300
3   7   2500
4   9   2800
5  13   2900
6  14   3400
7  16   3900
8  18  11000

We can use the scatterplot() function in seaborn to create a scatterplot that uses a linear scale on both the x-axis and y-axis:

import seaborn as sns

#create scatterplot with default axis scales
sns.scatterplot(data=df, x='x', y='y')

To use a log scale for the y-axis only, we can use the following syntax:

import matplotlib.pyplotas plt
import seaborn as sns

#create scatterplot with log scale on y-axis
sns.scatterplot(data=df, x='x', y='y')
plt.yscale('log')

seaborn log scale

Notice that the y-axis now uses a log scale.

We can also use a log scale on the x-axis if we’d like:

import matplotlib.pyplotas plt
import seaborn as sns

#create scatterplot with log scale on both axes
sns.scatterplot(data=df, x='x', y='y')
plt.yscale('log')
plt.xscale('log')

Notice that both axes now use a log scale.

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

Cite this article

stats writer (2024). How do I use a log scale in Seaborn plots?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-use-a-log-scale-in-seaborn-plots/

stats writer. "How do I use a log scale in Seaborn plots?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-i-use-a-log-scale-in-seaborn-plots/.

stats writer. "How do I use a log scale in Seaborn plots?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-use-a-log-scale-in-seaborn-plots/.

stats writer (2024) 'How do I use a log scale in Seaborn plots?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-use-a-log-scale-in-seaborn-plots/.

[1] stats writer, "How do I use a log scale in Seaborn plots?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How do I use a log scale in Seaborn plots?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)

Comments are closed.

Slide Up
x
PDF
Scroll to Top