Plot Log-Log Scatter and Histogram Charts in Matplotlib

Recently, I was working on a project where I needed to compare large U.S. economic datasets. Some of the values ranged from very small fractions to extremely large numbers.

When I plotted the data on a normal scale, the smaller values were almost invisible. That’s when I realized I needed log-log charts in Python Matplotlib.

Log-log charts are extremely useful when you are working with data that spans several orders of magnitude. They make patterns clearer and give you a better understanding of the relationships in your data.

In this tutorial, I will show you step-by-step how to create log-log scatter plots and log-log histograms in Python Matplotlib. I’ll cover two easy methods for each, with full code examples.

Plot Log-Log Scatter Chart in Python Matplotlib

Scatter plots are one of the most common ways to visualize data. When combined with log-log scaling, they become powerful tools for analyzing patterns in large datasets.

Below, I’ll show you two methods to create log-log scatter plots in Python using Matplotlib.

Method 1 – Use plt.scatter() with plt.xscale() and plt.yscale()

This is the simple method. You create a scatter plot and then set both axes to logarithmic scale.

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
# Example: U.S. household incomes (in thousands) vs. spending on healthcare
np.random.seed(42)
income = np.random.lognormal(mean=3.5, sigma=0.6, size=200) * 1000
healthcare_spending = income ** 0.7 + np.random.normal(0, 1000, 200)

# Create scatter plot
plt.figure(figsize=(8,6))
plt.scatter(income, healthcare_spending, alpha=0.7, color='blue')

# Apply log-log scaling
plt.xscale('log')
plt.yscale('log')

# Add labels and title
plt.xlabel("Household Income (USD)")
plt.ylabel("Healthcare Spending (USD)")
plt.title("Log-Log Scatter Plot of U.S. Income vs Healthcare Spending")

plt.grid(True, which="both", ls="--", linewidth=0.5)
plt.show()

You can refer to the screenshot below to see the output.

Plot Log-Log Scatter and Histogram Charts in Matplotlib

This method is simple and works well when you already have your scatter plot ready. You just need to switch both axes to a logarithmic scale.

Method 2 – Use plt.loglog() Function in Python

Another way is to directly use the loglog() function in Matplotlib. This function automatically sets both axes to log scale.

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
# Example: U.S. city population vs. number of tech startups
np.random.seed(7)
population = np.random.lognormal(mean=5, sigma=1, size=150)
startups = population ** 0.8 + np.random.normal(0, 500, 150)

# Create log-log scatter plot
plt.figure(figsize=(8,6))
plt.loglog(population, startups, 'o', alpha=0.7, color='green')

# Add labels and title
plt.xlabel("City Population")
plt.ylabel("Number of Tech Startups")
plt.title("Log-Log Scatter Plot of Population vs Startups in U.S. Cities")

plt.grid(True, which="both", ls="--", linewidth=0.5)
plt.show()

You can refer to the screenshot below to see the output.

How to Plot Log-Log Scatter and Histogram Charts in Matplotlib

This method is faster if you know from the start that you want a log-log scatter plot. You don’t need to set the axis scale separately.

Plot Log-Log Histogram in Python Matplotlib

Histograms are useful for understanding the distribution of data. But when your data spans a wide range, a log-log histogram makes the distribution much clearer.

Here, I’ll show you two methods to create log-log histograms in Python Matplotlib.

Method 1 – Use plt.hist() with Logarithmic Axes

The first method is to create a histogram normally and then apply log scaling to both axes.

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
# Example: U.S. company revenues (in millions USD)
np.random.seed(21)
revenues = np.random.lognormal(mean=4, sigma=1.2, size=1000)

# Create histogram
plt.figure(figsize=(8,6))
plt.hist(revenues, bins=50, color='purple', alpha=0.7)

# Apply log-log scaling
plt.xscale('log')
plt.yscale('log')

# Add labels and title
plt.xlabel("Company Revenue (Millions USD)")
plt.ylabel("Frequency")
plt.title("Log-Log Histogram of U.S. Company Revenues")

plt.grid(True, which="both", ls="--", linewidth=0.5)
plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Plot Log-Log Scatter and Histogram Charts

This method is flexible because you can first create your histogram and then decide whether to switch to log-log scaling.

Method 2 – Use plt.hist() with log=True and Custom Axes

Another option is to use the log=True parameter in plt.hist(). This applies a log scale to the y-axis automatically. Then, you can set the x-axis to log scale manually.

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
# Example: U.S. housing prices (in USD)
np.random.seed(99)
housing_prices = np.random.lognormal(mean=12, sigma=0.8, size=1200)

# Create histogram with log scale on y-axis
plt.figure(figsize=(8,6))
plt.hist(housing_prices, bins=60, color='orange', alpha=0.7, log=True)

# Apply log scale to x-axis
plt.xscale('log')

# Add labels and title
plt.xlabel("Housing Prices (USD)")
plt.ylabel("Frequency (Log Scale)")
plt.title("Log-Log Histogram of U.S. Housing Prices")

plt.grid(True, which="both", ls="--", linewidth=0.5)
plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Log-Log Scatter and Histogram Charts plot

This method is convenient when you want to quickly apply log scaling to the y-axis and then extend it to the x-axis.

Key Points to Remember

  • Use log-log scatter plots when you want to see relationships across multiple magnitudes.
  • Use log-log histograms when you want to analyze distributions with very large ranges.
  • plt.xscale(‘log’) and plt.yscale(‘log’) give you control, while plt.loglog() is a shortcut for scatter plots.
  • For histograms, log=True is useful but only applies to the y-axis by default.

Most of the time, I prefer the plt.loglog() function for scatter plots because it is faster and cleaner. For histograms, I usually go with plt.hist() and then set both axes to log scale.

Both approaches are correct; it depends on the type of dataset and how much control you want over the plot.

I hope you found this tutorial helpful. Try these methods with your own datasets, and you’ll see how much clearer your insights become when you use log-log scaling in Python Matplotlib.

You may also read other Matplotlib tutorials:

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.