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 make semilogx and semilogy plots in Matplotlib?
To make semilogx and semilogy plots in Matplotlib, you can use logarithmic scaling on one axis while keeping the other axis linear. This is useful for visualizing data with exponential relationships or wide value ranges.
Basic Steps
- Set the figure size and adjust the padding between and around the subplots
- Create a new figure or activate an existing figure
- Scatter and plot x and y data points
- Make a plot with log scaling on the X axis using
semilogx() - Make a plot with log scaling on the Y axis using
semilogy() - To display the figure, use show() method
Semilogx Plot (Log X-axis)
A semilogx plot has logarithmic scaling on the X-axis and linear scaling on the Y-axis ?
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.logspace(1, 5, 50) # Logarithmic space from 10^1 to 10^5
y = np.log10(x) # Linear relationship in log space
plt.figure(figsize=(8, 5))
plt.semilogx(x, y, 'b-', linewidth=2, label='Log relationship')
plt.xlabel('X (log scale)')
plt.ylabel('Y (linear scale)')
plt.title('Semilogx Plot')
plt.grid(True)
plt.legend()
plt.show()
Semilogy Plot (Log Y-axis)
A semilogy plot has linear scaling on the X-axis and logarithmic scaling on the Y-axis ?
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.linspace(0, 10, 50)
y = np.exp(x) # Exponential growth
plt.figure(figsize=(8, 5))
plt.semilogy(x, y, 'r-', linewidth=2, label='Exponential growth')
plt.xlabel('X (linear scale)')
plt.ylabel('Y (log scale)')
plt.title('Semilogy Plot')
plt.grid(True)
plt.legend()
plt.show()
Complete Example with Both Plot Types
Here's a comprehensive example showing both semilogx and semilogy plots with custom base values ?
import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = [10, 100, 1000, 10000, 100000]
y = [2, 4, 8, 16, 32]
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# Semilogx plot
ax1.scatter(x, y, color='blue', s=50)
ax1.plot(x, y, 'b-', linewidth=2)
ax1.set_xscale('log')
ax1.set_xlabel('X (log scale)')
ax1.set_ylabel('Y (linear scale)')
ax1.set_title('Semilogx Plot')
ax1.grid(True)
# Semilogy plot with base 2
ax2.scatter(x, y, color='red', s=50)
ax2.plot(x, y, 'r-', linewidth=2)
ax2.set_yscale('log', base=2)
ax2.set_xlabel('X (linear scale)')
ax2.set_ylabel('Y (log base 2 scale)')
ax2.set_title('Semilogy Plot (Base 2)')
ax2.grid(True)
plt.tight_layout()
plt.show()
Parameters
| Function | Purpose | Key Parameters |
|---|---|---|
semilogx() |
Log scale on X-axis |
basex (default: 10) |
semilogy() |
Log scale on Y-axis |
basey (default: 10) |
set_xscale('log') |
Alternative method for X-axis |
base parameter |
set_yscale('log') |
Alternative method for Y-axis |
base parameter |
Conclusion
Semilog plots are essential for visualizing exponential data or relationships spanning multiple orders of magnitude. Use semilogx() for logarithmic X-axis scaling and semilogy() for logarithmic Y-axis scaling, with optional custom base values.
