4 C
London
Friday, December 20, 2024
HomePythonMatplotlib in PythonHow to Create Matplotlib Plots with Log Scales

How to Create Matplotlib Plots with Log Scales

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...

Often you may want to create Matplotlib plots with log scales for one or more axes. Fortunately Matplotlib offers the following three functions for doing so:

This tutorial explains how to use each of these functions in practice.

Example 1: Log Scale for the X-Axis

Suppose we create a line chart for the following data:

import matplotlib.pyplot as plt

#create data
x = [1, 8, 190, 1400, 6500] 
y = [1, 2, 3, 4, 5]

#create line chart of data
plt.plot(x,y)

We can use the .semilogx() function to convert the x-axis to a log scale:

plt.semilogx()

Matplotlib plot with log scale on x-axis

Note that the y-axis is the exact same, but the x-axis is now on a log scale.

Example 2: Log Scale for the Y-Axis

Suppose we create a line chart for the following data:

import matplotlib.pyplot as plt

#create data
x = [1, 2, 3, 4, 5]
y = [1, 8, 190, 1400, 6500]

#create line chart of data
plt.plot(x,y)

We can use the .semilogy() function to convert the y-axis to a log scale:

plt.semilogy()

Matplotlib with log scale on y-axis

Note that the x-axis is the exact same, but the y-axis is now on a log scale.

Example 3: Log Scale for Both Axes

Suppose we create a line chart for the following data:

import matplotlib.pyplot as plt

#create data
x = [10, 200, 3000, 40000, 500000]
y = [30, 400, 5000, 60000, 750000]

#create line chart of data
plt.plot(x,y)

We can use the .loglog() function to convert the y-axis to a log scale:

plt.loglog(x, y)

Log log plot in Matplotlib

Note that both axes are now on a log scale.

Additional Resources

How to Change Font Sizes on a Matplotlib Plot
How to Remove Ticks from Matplotlib Plots

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