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 show the Logarithmic plot of a cumulative distribution function in Matplotlib?
To show the logarithmic plot of a cumulative distribution function (CDF) in Matplotlib, we need to create sample data, calculate the CDF, and apply logarithmic scaling to both axes. This visualization is useful for analyzing distributions that span several orders of magnitude.
Steps
- Set the figure size and adjust the padding between and around the subplots
- Initialize a variable N for the number of sample data points
- Create random sample data using NumPy
- Sort the data and calculate cumulative probabilities
- Plot the data using plot() method
- Apply logarithmic scaling to both x and y axes
- Display the figure using show() method
Example
Here's how to create a logarithmic CDF plot with normally distributed random 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
# Generate sample data
N = 100
data = np.random.randn(N)
# Sort data and calculate CDF
X2 = np.sort(data)
F2 = np.array(range(N)) / float(N)
# Create the plot
plt.plot(X2, F2, marker='o', markersize=3)
plt.xlabel('Data Values')
plt.ylabel('Cumulative Probability')
plt.title('Logarithmic Plot of Cumulative Distribution Function')
# Apply logarithmic scaling
plt.xscale('log')
plt.yscale('log')
plt.show()
Understanding the Code
The key components of this logarithmic CDF plot are ?
-
Data Generation:
np.random.randn(N)creates normally distributed random numbers -
Sorting:
np.sort(data)arranges values in ascending order for proper CDF calculation -
CDF Calculation:
np.array(range(N))/float(N)creates cumulative probabilities from 0 to 1 -
Log Scaling:
plt.xscale('log')andplt.yscale('log')apply logarithmic transformation
Working with Positive Data
Since logarithmic scales require positive values, here's an example using exponential data ?
import numpy as np
import matplotlib.pyplot as plt
# Generate positive exponential data
N = 200
data = np.random.exponential(scale=2.0, size=N)
# Sort and calculate CDF
X2 = np.sort(data)
F2 = (np.arange(N) + 1) / float(N)
# Create logarithmic CDF plot
plt.figure(figsize=(8, 5))
plt.plot(X2, F2, 'b-', linewidth=2, label='Exponential CDF')
plt.xlabel('Data Values (log scale)')
plt.ylabel('Cumulative Probability (log scale)')
plt.title('Log-Log Plot of Exponential CDF')
plt.grid(True, alpha=0.3)
plt.legend()
# Apply log scaling
plt.xscale('log')
plt.yscale('log')
plt.show()
Conclusion
Logarithmic CDF plots are essential for visualizing data distributions across multiple orders of magnitude. Use plt.xscale('log') and plt.yscale('log') to apply logarithmic scaling, and ensure your data contains only positive values for proper visualization.
