I first started working with Python for data visualization, and I often found myself needing to plot data that spanned several orders of magnitude.
In many real-world datasets, especially in finance, physics, or even U.S. population growth studies, the values don’t fit neatly into a linear scale. That’s when I realized the power of using logarithmic scales in Matplotlib.
In this tutorial, I’ll walk you through how to set log-log scales for both X and Y axes in Matplotlib. I’ll share the exact methods I use in my projects, along with complete Python code examples.
By the end of this guide, you’ll know how to:
- Set a log scale for the X-axis only.
- Set a log scale for the Y-axis only.
- Use Matplotlib’s loglog() function to set both axes to logarithmic scales.
Why Use Logarithmic Scales in Python Matplotlib?
Before we get into the code, let me explain why I use log scales.
A logarithmic scale is perfect when your data grows exponentially or spans multiple magnitudes. For example, stock market data, earthquake magnitudes, or internet traffic growth in the USA often follow exponential patterns.
If you try to plot such data on a linear scale, the smaller values get squashed, and you lose meaningful insights. But with a log scale, every order of magnitude gets equal spacing, making the visualization much more readable.
Method 1 – Set Log Scale for X-Axis in Matplotlib
Sometimes, I only want to apply a logarithmic scale to the X-axis while keeping the Y-axis linear. This is useful when I’m analyzing time-series data where the time intervals grow exponentially.
Here’s how you can do it in Python.
import matplotlib.pyplot as plt
import numpy as np
# Sample dataset: exponential growth (e.g., US internet users over years)
x = np.logspace(0.1, 2, 100) # X values on log scale
y = x ** 2 # Quadratic growth
plt.figure(figsize=(8, 6))
plt.plot(x, y, marker="o", linestyle="-", color="blue")
# Set log scale for X-axis
plt.xscale("log")
plt.title("Log Scale for X-Axis Example (Python Matplotlib)")
plt.xlabel("Logarithmic X-Axis")
plt.ylabel("Linear Y-Axis")
plt.grid(True, which="both", linestyle="--", linewidth=0.7)
plt.show()You can refer ot the screenshot below to see the output.

In this code, I used plt.xscale(“log”) to convert the X-axis into a logarithmic scale. Notice how the X-axis values are spaced evenly across powers of 10, while the Y-axis remains linear.
Method 2 – Set Log Scale for Y-Axis in Matplotlib
In other cases, I only need the Y-axis to be logarithmic. For example, when plotting U.S. household income distribution, the frequency of higher incomes drops exponentially.
Here’s how I handle that in Python.
import matplotlib.pyplot as plt
import numpy as np
# Sample dataset: exponential decay (e.g., income distribution in the USA)
x = np.linspace(1, 100, 100)
y = np.exp(-0.1 * x) * 1000 # Exponential decay
plt.figure(figsize=(8, 6))
plt.plot(x, y, marker="s", linestyle="--", color="red")
# Set log scale for Y-axis
plt.yscale("log")
plt.title("Log Scale for Y-Axis Example (Python Matplotlib)")
plt.xlabel("Linear X-Axis")
plt.ylabel("Logarithmic Y-Axis")
plt.grid(True, which="both", linestyle="--", linewidth=0.7)
plt.show()You can refer ot the screenshot below to see the output.

Here, plt.yscale(“log”) transforms the Y-axis into a logarithmic scale. This makes it easier to see patterns in data that decay quickly, instead of having all the small values squeezed at the bottom.
Method 3 – Use Log-Log Scale for Both X and Y Axes in Matplotlib
Now let’s look at the most common case: applying a log scale to both X and Y axes.
This is especially useful when analyzing power-law distributions, such as earthquake magnitudes, city populations, or even U.S. tech startup valuations.
Python’s Matplotlib provides a direct function called loglog() that makes this very simple.
import matplotlib.pyplot as plt
import numpy as np
# Sample dataset: Power law distribution
x = np.logspace(0.1, 2, 100) # X values from 10^0.1 to 10^2
y = x ** 3 # Cubic growth
plt.figure(figsize=(8, 6))
# Use loglog() to set both axes to log scale
plt.loglog(x, y, marker="^", linestyle="-.", color="green")
plt.title("Log-Log Scale Example in Python Matplotlib")
plt.xlabel("Logarithmic X-Axis")
plt.ylabel("Logarithmic Y-Axis")
plt.grid(True, which="both", linestyle="--", linewidth=0.7)
plt.show()You can refer ot the screenshot below to see the output.

By using plt.loglog(), I don’t need to separately call plt.xscale(“log”) and plt.yscale(“log”). This method is concise and perfect when both axes need logarithmic scaling.
Compare the Three Methods
Here’s a quick summary of when to use each method:
- X-axis log scale only → Best for datasets where time or input grows exponentially.
- Y-axis log scale only → Best for datasets where the output decays exponentially.
- Log-log scale for both axes → Best for power-law relationships or exponential growth in both dimensions.
Practical Use Cases in the USA
Let me share a few real-world scenarios where I’ve applied these techniques in Python:
- Finance: Plotting U.S. stock market data, where returns often follow log-normal distributions.
- Population Studies: Analyzing U.S. city populations, which follow a power-law distribution.
- Tech Growth: Visualizing the exponential rise of internet users in the U.S. from the 1990s to today.
- Seismology: Studying earthquake magnitudes in California, where log-log plots reveal distribution patterns.
These are just a few examples where log-log scales make the data much clearer and more insightful.
Tips for Working with Logarithmic Scales in Python
Here are some quick tips I’ve learned over the years:
- Avoid zero or negative values – log scales don’t work with them.
- Use np.logspace() for generating data – it creates evenly spaced values on a log scale.
- Always enable grid lines – use plt.grid(True, which=”both”) to make the log ticks clearer.
- Label your axes properly – always mention that the axis is logarithmic to avoid confusion.
- Experiment with base values – by default, Matplotlib uses base 10, but you can set other bases if needed.
Working with log-log scales in Matplotlib has been a game-changer for my data analysis in Python.
By knowing when to apply log scales to the X-axis, Y-axis, or both, I can reveal hidden patterns in datasets that would otherwise look flat or misleading.
Whether you’re analyzing U.S. financial data, population growth, or scientific experiments, Python’s Matplotlib makes it simple to switch between linear and logarithmic scales.
You may also like to read:
- Matplotlib log-log: Use Base 2 and Handle Negative Values
- Plot Log-Log Scatter and Histogram Charts in Matplotlib
- Plot Log-Log Plots with Error Bars and Grid Using Matplotlib
- Why is matplotlib subplots_adjust Not Working in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.