When I first started using Python Matplotlib, I was fascinated by how easily I could transform simple data into meaningful visual insights. Over the years, I’ve realized that logarithmic scales are essential when dealing with data that spans several orders of magnitude.
In this tutorial, I’ll walk you through how I use Matplotlib’s log‑log scale with minor ticks and colorbars. I’ll share two methods for each so you can choose the one that fits best for your project.
Whether you’re analyzing financial data, environmental metrics, or scientific experiments, understanding how to plot data on a log‑log scale can make your visualizations more accurate and visually appealing.
What Is a Log‑Log Scale in Python Matplotlib?
A log‑log scale means both the X and Y axes are represented on a logarithmic scale. This helps visualize data that grows exponentially or covers a wide range of values.
In Python Matplotlib, you can create a log‑log plot using the plt.loglog() function or by setting both axes to a log scale using plt.xscale(‘log’) and plt.yscale(‘log’).
Log Scale with Colorbar in Python Matplotlib
When I visualize data intensity or magnitude differences, adding a colorbar helps me interpret the results quickly. A colorbar represents the mapping between color and data values, making your log‑log plot more informative.
Below, I’ll show you two methods to add a colorbar to a log‑log scale plot in Python Matplotlib.
Method 1: Use plt.loglog() with plt.scatter() and Colorbar
This is my preferred method when I want to plot scattered data points on a log‑log scale and visualize color variations based on another variable.
Here’s the full Python code example:
import numpy as np
import matplotlib.pyplot as plt
# Generate synthetic data
x = np.logspace(1, 5, 100)
y = np.logspace(2, 6, 100)
z = np.random.rand(100) * 100 # Color intensity
# Create a scatter plot on a log-log scale
plt.figure(figsize=(8, 6))
scatter = plt.scatter(x, y, c=z, cmap='viridis', s=60, edgecolor='black')
# Set log-log scale
plt.xscale('log')
plt.yscale('log')
# Add colorbar
cbar = plt.colorbar(scatter)
cbar.set_label('Intensity Level', rotation=270, labelpad=15)
# Add labels and title
plt.title('Log-Log Scatter Plot with Colorbar in Python Matplotlib')
plt.xlabel('Log Scale X-axis')
plt.ylabel('Log Scale Y-axis')
# Show the plot
plt.tight_layout()
plt.show()I executed the above example code and added the screenshot below.

I use np.logspace() to generate exponentially spaced data points, which are ideal for log‑log plotting. The colorbar helps me interpret the magnitude of each point easily.
Method 2: Use imshow() for Logarithmic Color Mapping
Sometimes, I work with 2D data grids, like heatmaps or density plots. In such cases, using imshow() with logarithmic normalization gives a clear visual representation.
Here’s how I do it:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
# Create a 2D grid of data
x = np.linspace(1, 100, 200)
y = np.linspace(1, 100, 200)
X, Y = np.meshgrid(x, y)
Z = X**2 + Y**2
# Create a plot with logarithmic color normalization
plt.figure(figsize=(8, 6))
img = plt.imshow(Z, norm=LogNorm(vmin=Z.min(), vmax=Z.max()), cmap='plasma')
# Add colorbar
cbar = plt.colorbar(img)
cbar.set_label('Logarithmic Intensity', rotation=270, labelpad=15)
plt.title('Logarithmic Colorbar Example in Python Matplotlib')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.tight_layout()
plt.show()I executed the above example code and added the screenshot below.

Here, I apply LogNorm() to normalize the color mapping logarithmically. This approach is perfect for visualizing data that varies exponentially across a grid.
Log Scale with Minor Ticks in Python Matplotlib
Minor ticks help highlight intermediate values between major tick marks, providing more detail in your visualization.
When using logarithmic scales, Matplotlib can automatically generate minor ticks, but sometimes I prefer customizing them for better readability.
Let’s look at two practical methods to add and customize minor ticks in a log‑log scale plot.
Method 1: Enable Automatic Minor Ticks
This is the simplest way to add minor ticks to a log‑log plot. Matplotlib automatically places them based on the data range.
Here’s how I do it:
import numpy as np
import matplotlib.pyplot as plt
# Sample data
x = np.logspace(0, 4, 100)
y = x ** 2
plt.figure(figsize=(8, 6))
plt.loglog(x, y, color='teal', linewidth=2)
# Enable minor ticks
plt.minorticks_on()
# Customize tick parameters
plt.tick_params(which='both', width=1)
plt.tick_params(which='major', length=8)
plt.tick_params(which='minor', length=4, color='red')
plt.title('Log-Log Plot with Automatic Minor Ticks in Python Matplotlib')
plt.xlabel('Log Scale X-axis')
plt.ylabel('Log Scale Y-axis')
plt.grid(which='both', linestyle='--', linewidth=0.5)
plt.tight_layout()
plt.show()I executed the above example code and added the screenshot below.

In this example, I enable minor ticks using plt.minorticks_on() and customize their appearance using tick_params(). The red minor ticks make it easier to differentiate between major and minor grid lines.
Method 2: Manually Setting Minor Ticks with LogLocator
When I need more control over tick placement, I use the LogLocator class from Matplotlib’s ticker module. This method gives me the flexibility to define how many minor ticks appear between major ticks.
Here’s the Python code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import LogLocator
# Generate data
x = np.logspace(0, 5, 200)
y = x ** 1.5
fig, ax = plt.subplots(figsize=(8, 6))
ax.loglog(x, y, color='purple', linewidth=2)
# Customize minor ticks using LogLocator
ax.xaxis.set_minor_locator(LogLocator(base=10.0, subs=np.arange(2, 10)*0.1))
ax.yaxis.set_minor_locator(LogLocator(base=10.0, subs=np.arange(2, 10)*0.1))
# Enable grid for both major and minor ticks
ax.grid(which='major', linestyle='-', linewidth=0.7)
ax.grid(which='minor', linestyle='--', linewidth=0.5, color='gray')
ax.set_title('Custom Minor Ticks on Log-Log Plot in Python Matplotlib')
ax.set_xlabel('Log Scale X-axis')
ax.set_ylabel('Log Scale Y-axis')
plt.tight_layout()
plt.show()I executed the above example code and added the screenshot below.

Here, I use LogLocator() to manually define minor tick intervals. This gives me precise control over tick spacing, which is especially helpful when presenting data in professional reports.
Combine Log‑Log Scale, Minor Ticks, and Colorbar
In real‑world projects, I often combine all these elements, log‑log scaling, colorbars, and minor ticks, to create advanced, publication‑ready visualizations.
Here’s a complete Python example combining all three:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from matplotlib.ticker import LogLocator
# Generate data
x = np.logspace(1, 5, 200)
y = np.logspace(1, 5, 200)
X, Y = np.meshgrid(x, y)
Z = X**2 + Y**3
# Create the plot
fig, ax = plt.subplots(figsize=(8, 6))
img = ax.pcolormesh(X, Y, Z, norm=LogNorm(vmin=Z.min(), vmax=Z.max()), cmap='viridis', shading='auto')
# Set log-log scale
ax.set_xscale('log')
ax.set_yscale('log')
# Add colorbar
cbar = plt.colorbar(img, ax=ax)
cbar.set_label('Logarithmic Intensity', rotation=270, labelpad=15)
# Add minor ticks
ax.xaxis.set_minor_locator(LogLocator(base=10.0, subs=np.arange(2, 10)*0.1))
ax.yaxis.set_minor_locator(LogLocator(base=10.0, subs=np.arange(2, 10)*0.1))
# Add grid
ax.grid(which='both', linestyle='--', linewidth=0.5)
ax.set_title('Log-Log Scale Plot with Minor Ticks and Colorbar in Python Matplotlib')
ax.set_xlabel('Log Scale X-axis')
ax.set_ylabel('Log Scale Y-axis')
plt.tight_layout()
plt.show()This example combines all the techniques we’ve discussed, resulting in a clean and informative log‑log plot. It’s ideal for datasets that span multiple orders of magnitude, such as population growth, income distribution, or scientific measurements.
Conclusion
Working with log‑log scales in Python Matplotlib has been a game‑changer in my data visualization workflow. By adding minor ticks and colorbars, I can create plots that are not only visually appealing but also rich in information.
Whether you’re a data analyst, researcher, or engineer, mastering these techniques will help you present complex data in a clear, professional manner.
Try these methods on your own datasets. Once you get comfortable, you’ll find log‑log plots indispensable in your Python projects.
You may also read:
- Matplotlib Horizontal Line with Text in Python
- Remove a Horizontal Line in Matplotlib using Python
- Matplotlib Bar Chart with Different Colors in Python
- Plot a Bar Chart from a Dictionary in Python Matplotlib

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.