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
Logarithmic Y-axis bins in Python
To plot logarithmic Y-axis bins in Python, we can use matplotlib's yscale() method to set a logarithmic scale. This is particularly useful when your data spans several orders of magnitude, making it easier to visualize trends that would be compressed on a linear scale.
Steps to Create Logarithmic Y-axis Plot
Create x and y data points using NumPy
Set the Y-axis scale using the yscale() method
Plot the x and y points using the plot() method
Add labels and legend for better visualization
Display the figure using the show() method
Example
Here's how to create a logarithmic Y-axis plot with exponential data ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure parameters
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create data points
x = np.linspace(1, 100, 1000)
y = np.exp(x/20) # Exponential function for better log scale demonstration
# Set logarithmic scale for Y-axis
plt.yscale('log')
# Plot the data
plt.plot(x, y, c="red", lw=3, linestyle="dashdot", label="y=exp(x/20)")
plt.xlabel("X values")
plt.ylabel("Y values (log scale)")
plt.legend()
plt.show()
Linear vs Logarithmic Comparison
Let's compare the same data on both linear and logarithmic Y-axis scales ?
import numpy as np
import matplotlib.pyplot as plt
# Create exponential data
x = np.linspace(0, 5, 100)
y = np.exp(x)
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
# Linear scale plot
ax1.plot(x, y, 'b-', linewidth=2)
ax1.set_title('Linear Y-axis')
ax1.set_xlabel('X values')
ax1.set_ylabel('Y values')
ax1.grid(True, alpha=0.3)
# Logarithmic scale plot
ax2.plot(x, y, 'r-', linewidth=2)
ax2.set_yscale('log')
ax2.set_title('Logarithmic Y-axis')
ax2.set_xlabel('X values')
ax2.set_ylabel('Y values (log scale)')
ax2.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Customizing Logarithmic Scale
You can customize the logarithmic scale with different bases and formatting options ?
import numpy as np
import matplotlib.pyplot as plt
# Generate data with wide range
x = np.linspace(1, 50, 100)
y1 = 10**x # Base 10 exponential
y2 = 2**x # Base 2 exponential
plt.figure(figsize=(8, 6))
# Plot with logarithmic Y-axis
plt.plot(x, y1, 'b-', label='10^x', linewidth=2)
plt.plot(x, y2, 'r--', label='2^x', linewidth=2)
# Set log scale with base 10
plt.yscale('log', base=10)
plt.xlabel('X values')
plt.ylabel('Y values (log?? scale)')
plt.title('Logarithmic Y-axis with Different Functions')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
When to Use Logarithmic Scale
| Data Type | Linear Scale | Log Scale |
|---|---|---|
| Small range (1-100) | ? Better | ? Not needed |
| Large range (1-10?) | ? Hard to read | ? Much better |
| Exponential growth | ? Compressed | ? Shows trend clearly |
Conclusion
Use logarithmic Y-axis scaling when your data spans multiple orders of magnitude or exhibits exponential behavior. The yscale('log') method transforms compressed exponential curves into clear linear trends, making data analysis much easier.
