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
How to have logarithmic bins in a Python histogram?
In Python, creating a logarithmic histogram involves using logarithmically spaced bins instead of linear ones. This is particularly useful when your data spans several orders of magnitude. We can achieve this using NumPy for generating logarithmic bins and matplotlib for plotting.
Logarithmic bins are spaced exponentially rather than linearly, making them ideal for data that follows power-law distributions or spans wide ranges.
Basic Example with Logarithmic Bins
Let's create a simple histogram with logarithmic bins ?
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
data = np.random.exponential(scale=2, size=1000)
# Create logarithmic bins
bins = np.logspace(np.log10(0.1), np.log10(20), 20)
# Create histogram
plt.hist(data, bins=bins, alpha=0.7, edgecolor='black')
plt.xscale('log')
plt.xlabel('Value (log scale)')
plt.ylabel('Frequency')
plt.title('Histogram with Logarithmic Bins')
plt.grid(True, alpha=0.3)
plt.show()
Using np.logspace() for Bin Creation
The key is using np.logspace() to create logarithmically spaced bin edges ?
import matplotlib.pyplot as plt
import numpy as np
# Generate data spanning multiple orders of magnitude
np.random.seed(42)
data = np.concatenate([
np.random.normal(1, 0.5, 300),
np.random.normal(10, 2, 400),
np.random.normal(100, 20, 300)
])
# Create logarithmic bins from 0.1 to 1000
log_bins = np.logspace(-1, 3, 30) # 10^(-1) to 10^3 with 30 bins
plt.figure(figsize=(10, 6))
plt.hist(data, bins=log_bins, alpha=0.7, color='skyblue', edgecolor='black')
plt.xscale('log')
plt.xlabel('Value (logarithmic scale)')
plt.ylabel('Frequency')
plt.title('Histogram with Logarithmic Bins using np.logspace()')
plt.grid(True, alpha=0.3)
plt.show()
Comparison: Linear vs Logarithmic Bins
Here's a side-by-side comparison showing the difference ?
import matplotlib.pyplot as plt
import numpy as np
# Generate power-law distributed data
np.random.seed(42)
data = np.random.pareto(a=1, size=1000) + 1
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Linear bins
ax1.hist(data, bins=30, alpha=0.7, color='red', edgecolor='black')
ax1.set_xlabel('Value (linear scale)')
ax1.set_ylabel('Frequency')
ax1.set_title('Linear Bins')
ax1.grid(True, alpha=0.3)
# Logarithmic bins
log_bins = np.logspace(0, np.log10(data.max()), 30)
ax2.hist(data, bins=log_bins, alpha=0.7, color='blue', edgecolor='black')
ax2.set_xscale('log')
ax2.set_xlabel('Value (log scale)')
ax2.set_ylabel('Frequency')
ax2.set_title('Logarithmic Bins')
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Function | Description |
|---|---|---|
start |
np.logspace() |
Starting exponent (10^start) |
stop |
np.logspace() |
Ending exponent (10^stop) |
num |
np.logspace() |
Number of bins to create |
bins |
plt.hist() |
Array of bin edges |
Common Use Cases
Financial Data: Stock prices, market capitalizations
Scientific Data: Earthquake magnitudes, stellar brightness
Network Analysis: Node degrees, connection counts
Population Studies: City sizes, income distributions
Conclusion
Use np.logspace() to create logarithmic bins when your data spans multiple orders of magnitude. Set plt.xscale('log') to display the x-axis logarithmically for better visualization of wide-range data.
