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 exponentially scale the Y axis with matplotlib?
To exponentially scale the Y-axis with matplotlib, you can use the yscale() function with different scale types. The most common approach is using symlog (symmetric logarithmic) scaling, which handles both positive and negative values effectively.
Basic Exponential Y-axis Scaling
Here's how to create a plot with exponential Y-axis scaling ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create data points
dt = 0.01
x = np.arange(-50.0, 50.0, dt)
y = np.arange(0, 100.0, dt)
# Plot the data
plt.plot(x, y)
# Set exponential scale for Y-axis
plt.yscale('symlog')
# Add labels for clarity
plt.xlabel('X values')
plt.ylabel('Y values (symlog scale)')
plt.title('Exponential Y-axis Scaling')
plt.show()
Different Scale Types
You can use different scale types depending on your data ?
import numpy as np
import matplotlib.pyplot as plt
# Create exponential data
x = np.linspace(1, 10, 100)
y = np.exp(x)
# Create subplots for comparison
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 4))
# Linear scale
ax1.plot(x, y)
ax1.set_title('Linear Scale')
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
# Log scale
ax2.plot(x, y)
ax2.set_yscale('log')
ax2.set_title('Log Scale')
ax2.set_xlabel('X')
ax2.set_ylabel('Y (log scale)')
# Symlog scale
ax3.plot(x, y)
ax3.set_yscale('symlog')
ax3.set_title('Symlog Scale')
ax3.set_xlabel('X')
ax3.set_ylabel('Y (symlog scale)')
plt.tight_layout()
plt.show()
Scale Options
| Scale Type | Function | Best For |
|---|---|---|
| Linear | plt.yscale('linear') |
Normal data ranges |
| Logarithmic | plt.yscale('log') |
Positive values only |
| Symmetric Log | plt.yscale('symlog') |
Wide ranges with positive/negative values |
Customizing Symlog Parameters
You can fine-tune the symlog scaling with additional parameters ?
import numpy as np
import matplotlib.pyplot as plt
# Create data with wide range
x = np.linspace(-10, 10, 1000)
y = x**3
plt.figure(figsize=(8, 6))
plt.plot(x, y, 'b-', linewidth=2)
# Symlog with custom threshold
plt.yscale('symlog', linthresh=10)
plt.xlabel('X values')
plt.ylabel('Y values (symlog scale)')
plt.title('Custom Symlog Scaling with Threshold=10')
plt.grid(True, alpha=0.3)
plt.show()
Conclusion
Use plt.yscale('symlog') for exponential Y-axis scaling that handles both positive and negative values. For positive-only data, plt.yscale('log') provides traditional logarithmic scaling.
Advertisements
