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
Plot yscale class linear, log, logit and symlog by name in Matplotlib?
Matplotlib provides several Y-axis scaling options to better visualize data with different characteristics. The yscale() method allows you to apply linear, log, symlog, and logit scales by name to transform how data appears on the Y-axis.
Setting Up the Data
First, let's create sample data and configure the plot layout ?
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Generate sample data
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))
print(f"Data range: {y.min():.3f} to {y.max():.3f}")
print(f"Number of points: {len(y)}")
Data range: 0.001 to 0.999 Number of points: 680
Linear Scale
The default linear scale displays data with equal spacing ?
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))
# Linear scale
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('Linear Scale')
# Log scale
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('Log Scale')
# Symmetric log scale
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthreshy=0.01)
plt.title('Symlog Scale')
# Logit scale
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('Logit Scale')
plt.show()
Scale Types Comparison
| Scale Type | Best For | Data Range | Special Parameters |
|---|---|---|---|
'linear' |
Regular data | Any range | None |
'log' |
Exponential growth | Positive values only | None |
'symlog' |
Data with wide range including zero | Any range | linthreshy |
'logit' |
Probability data | 0 to 1 (exclusive) | None |
Key Parameters
The yscale() method accepts different parameters based on the scale type ?
import numpy as np
import matplotlib.pyplot as plt
# Example with symlog parameters
data = np.array([-100, -10, -1, 0, 1, 10, 100, 1000])
x = np.arange(len(data))
plt.figure(figsize=(10, 4))
plt.subplot(121)
plt.plot(x, data, 'o-')
plt.yscale('symlog', linthreshy=1)
plt.title('Symlog (linthreshy=1)')
plt.grid(True)
plt.subplot(122)
plt.plot(x, data, 'o-')
plt.yscale('symlog', linthreshy=10)
plt.title('Symlog (linthreshy=10)')
plt.grid(True)
plt.tight_layout()
plt.show()
Conclusion
Use plt.yscale() to transform Y-axis scaling based on your data characteristics. Linear for regular data, log for exponential patterns, symlog for wide ranges including zero, and logit for probability data between 0 and 1.
Advertisements
