Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to remove scientific notation from a Matplotlib log-log plot?
When creating log-log plots in Matplotlib, the axes often display values in scientific notation by default. To display regular decimal numbers instead, you can use the ScalarFormatter from matplotlib's ticker module.
Basic Approach
The key is to apply ScalarFormatter() to both major and minor ticks on the logarithmic axes ?
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
# Create sample data
x = np.array([1, 10, 100, 1000, 10000])
y = np.array([2, 20, 200, 2000, 20000])
# Create the plot
plt.figure(figsize=(8, 6))
plt.scatter(x, y, c='blue', s=50)
# Set log scale for both axes
ax = plt.gca()
ax.set_xscale('log')
ax.set_yscale('log')
# Remove scientific notation
ax.xaxis.set_major_formatter(mticker.ScalarFormatter())
ax.yaxis.set_major_formatter(mticker.ScalarFormatter())
ax.xaxis.set_minor_formatter(mticker.NullFormatter())
ax.yaxis.set_minor_formatter(mticker.NullFormatter())
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Log-Log Plot without Scientific Notation')
plt.grid(True, alpha=0.3)
plt.show()
Complete Example with Custom Data
Here's a more practical example with realistic data ?
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
# Generate sample data
x = np.logspace(0, 4, 20) # 1 to 10000
y = x**1.5 + np.random.normal(0, x*0.1)
plt.figure(figsize=(9, 6))
plt.scatter(x, y, alpha=0.7, c='red', edgecolors='black', s=40)
ax = plt.gca()
ax.set_xscale('log')
ax.set_yscale('log')
# Format both axes to show regular numbers
ax.xaxis.set_major_formatter(mticker.ScalarFormatter())
ax.yaxis.set_major_formatter(mticker.ScalarFormatter())
# Optional: disable minor tick labels to avoid clutter
ax.xaxis.set_minor_formatter(mticker.NullFormatter())
ax.yaxis.set_minor_formatter(mticker.NullFormatter())
plt.xlabel('X Values (log scale)')
plt.ylabel('Y Values (log scale)')
plt.title('Power Law Relationship - No Scientific Notation')
plt.grid(True, which="both", alpha=0.3)
plt.tight_layout()
plt.show()
Key Methods
| Method | Purpose | Usage |
|---|---|---|
ScalarFormatter() |
Display regular decimal numbers | Major tick formatting |
NullFormatter() |
Hide minor tick labels | Reduce visual clutter |
set_xscale('log') |
Set logarithmic scale | Create log-log plot |
Conclusion
Use ScalarFormatter() on both axes to remove scientific notation from log-log plots. Combine with NullFormatter() on minor ticks to keep the plot clean and readable.
Advertisements
