Work with Loglog Log Scale and Adjusting Ticks in Matplotlib

When I first started working with Python Matplotlib, I often struggled to make sense of data that spanned several orders of magnitude. If you’ve ever plotted data where values range from tiny decimals to very large numbers, you know how messy the chart can look in a normal linear scale.

That’s when I discovered the power of logarithmic scales in Matplotlib. By plotting data on a log-log scale, patterns and trends suddenly became much clearer.

In this tutorial, I’ll share how I work with log-log scales and how I adjust ticks in Matplotlib. I’ll walk you through different methods, with full Python code examples that you can run directly.

Work with Log-Log Scale in Matplotlib

When I want to plot data where both the x-axis and y-axis should be logarithmic, I use the loglog() function in Matplotlib.

This is especially useful for datasets like earthquake magnitudes, internet speeds, or financial returns that grow exponentially.

Method 1 – Use plt.loglog() in Python

The simplest way to create a log-log plot in Python Matplotlib is by using the built-in loglog() function.

Here’s a complete example where I plot a power-law relationship:

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
x = np.logspace(1, 4, 100)  # Values from 10^1 to 10^4
y = x ** 2                  # Quadratic relationship

# Create log-log plot
plt.figure(figsize=(8,6))
plt.loglog(x, y, marker='o', linestyle='-', color='blue', label='y = x^2')

# Add labels and title
plt.xlabel("X values (log scale)")
plt.ylabel("Y values (log scale)")
plt.title("Log-Log Plot Example in Python")
plt.legend()
plt.grid(True, which="both", linestyle="--")

plt.show()

You can see the output in the screenshot below.

Loglog Log Scale and Adjusting Ticks in Matplotlib

This method is easy, and I often use it when I need both axes to be logarithmic at once. The grid lines also adapt to the log scale, making it easier to read the chart.

Method 2 – Use Python’s plt.xscale() and plt.yscale()

Sometimes I prefer more flexibility, so instead of loglog(), I use xscale() and yscale(). This gives me control to set one axis as log and keep the other linear, or set both as log when needed.

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
x = np.linspace(1, 1000, 100)
y = np.exp(x/200)  # Exponential growth

# Create figure
plt.figure(figsize=(8,6))
plt.plot(x, y, color='green', label='Exponential Growth')

# Apply log scale to y-axis only
plt.yscale('log')

# Add labels and title
plt.xlabel("X values (linear scale)")
plt.ylabel("Y values (log scale)")
plt.title("Semi-Log Plot in Python (Y-axis Log)")
plt.legend()
plt.grid(True, which="both", linestyle="--")

plt.show()

You can see the output in the screenshot below.

Work with Loglog Log Scale and Adjusting Ticks in Matplotlib

In this method, I often use a semi-log scale when one variable grows exponentially while the other increases linearly.

Adjust Ticks in Matplotlib

Once I set up log scales, I usually notice that the ticks don’t always look clean.

Sometimes the ticks overlap, or they don’t show the values I want. That’s when adjusting ticks becomes very important.

Method 1 – Use plt.tick_params() in Python

The tick_params() function in Python Matplotlib lets me customize tick size, direction, and visibility.

Here’s an example where I increase tick size and rotate labels for better readability:

import matplotlib.pyplot as plt
import numpy as np

# Data
x = np.logspace(0.1, 2, 100)
y = x ** 1.5

# Log-log plot
plt.figure(figsize=(8,6))
plt.loglog(x, y, color='purple')

# Customize ticks
plt.tick_params(axis='both', which='major', labelsize=12, direction='in', length=8)
plt.tick_params(axis='both', which='minor', labelsize=8, direction='out', length=4)

# Labels and title
plt.xlabel("X values")
plt.ylabel("Y values")
plt.title("Customizing Ticks with tick_params() in Python")

plt.show()

You can see the output in the screenshot below.

Matplotlib Loglog Log Scale and Adjusting Ticks

This method is quick and effective when I want to improve the overall readability of the log-log chart. I use it often when preparing plots for reports or presentations.

Method 2 – Use LogLocator from Matplotlib

For more precise control, I use LogLocator from matplotlib.ticker. This allows me to define exactly where major and minor ticks should appear on a log scale.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import LogLocator

# Data
x = np.logspace(0.1, 3, 200)
y = x ** 0.5

# Log-log plot
plt.figure(figsize=(8,6))
plt.loglog(x, y, color='red')

# Use LogLocator for custom ticks
from matplotlib.ticker import LogLocator
plt.gca().xaxis.set_major_locator(LogLocator(base=10.0, subs=None, numticks=5))
plt.gca().yaxis.set_minor_locator(LogLocator(base=10.0, subs=np.arange(2, 10) * 0.1, numticks=10))

# Labels and title
plt.xlabel("X values")
plt.ylabel("Y values")
plt.title("Custom Tick Placement with LogLocator in Python")

plt.show()

You can see the output in the screenshot below.

Work with Matplotlib oglog Log Scale and Adjusting Ticks

This method is powerful when I want specific tick marks at certain intervals. It’s especially useful for scientific data analysis where precision is key.

Conclusion

Working with log-log scales in Matplotlib has completely changed how I visualize complex datasets in Python. By combining log scales with custom tick adjustments, I can create charts that are not only accurate but also easy to read.

I recommend starting with plt.loglog() for quick plots, and then moving to xscale()/yscale() when you need flexibility. For ticks, tick_params() is great for quick styling, while LogLocator gives you advanced control.

If you work with data that spans multiple magnitudes, like financial growth, scientific measurements, or even USA population studies, you’ll find these methods extremely helpful.

You may also like to read:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.