Calculate and Plot a Cumulative Distribution function with Matplotlib in Python Last Updated : 19 May, 2025 Comments Improve Suggest changes Like Article Like Report Cumulative Distribution Functions (CDFs) show the probability that a variable is less than or equal to a value, helping us understand data distribution. For example, a CDF of test scores reveals the percentage of students scoring below a certain mark. Let’s explore simple and efficient ways to calculate and plot CDFs using Matplotlib in Python.Using np.arangeThis is a simple way to compute the CDF. First, the data is sorted and then np.arange is used to create evenly spaced cumulative probabilities. It's fast and perfect when you want a clean and intuitive CDF without extra dependencies. Python import numpy as np import matplotlib.pyplot as plt d = np.sort(np.random.randn(500)) c = np.arange(1, len(d)+1) / len(d) plt.plot(d, c, '.', color='blue') plt.xlabel('Data Values') plt.ylabel('CDF') plt.title('CDF via Sorting') plt.grid() plt.show() OutputUsing np.arangeExplanation:np.random.randn(500) generates 500 random data points from a standard normal distribution (mean = 0, std = 1).np.sort(d) sorts the generated data in ascending order to prepare for cumulative comparison.np.arange(1, len(d)+1) / len(d) creates evenly spaced cumulative probabilities ranging from 1/500 to 500/500 .plt.plot(d, c, '.', color='blue') plots the sorted data values against their corresponding CDF values as blue dots on the graph.Using statsmodels ECDF Method statsmodels library provides a built-in class ECDF for empirical CDFs. It’s very convenient and gives you a ready-to-use CDF function. If you're okay with an extra dependency, this is one of the cleanest and most accurate methods. Python import numpy as np import matplotlib.pyplot as plt from statsmodels.distributions.empirical_distribution import ECDF d = np.random.randn(500) e = ECDF(d) plt.step(e.x, e.y, color='green') plt.xlabel('Data Values') plt.ylabel('CDF') plt.title('CDF via ECDF') plt.grid() plt.show() OutputUsing statsmodels ECDF Method Explanation:np.random.randn(500) generates 500 random data points from a standard normal distribution (mean = 0, std = 1).ECDF(d) computes the empirical cumulative distribution function of the data d.plt.step(e.x, e.y, color='green') plots the ECDF as a step function with green color, showing the cumulative probabilities.Using scipy.stats.cumfreq methodscipy.stats.cumfreq computes cumulative frequency tables. This method is slightly more advanced, offering more control over binning and output values. It's a good option if you already use SciPy in your workflow. Python import numpy as np import matplotlib.pyplot as plt from scipy.stats import cumfreq d = np.random.randn(500) r = cumfreq(d, numbins=25) x = r.lowerlimit + np.linspace(0, r.binsize * r.cumcount.size, r.cumcount.size) c = r.cumcount / r.cumcount[-1] plt.plot(x, c, color='purple') plt.xlabel('Data Values') plt.ylabel('CDF') plt.title('CDF via cumfreq') plt.grid() plt.show() OutputUsing scipy.stats.cumfreq Explanation:np.random.randn(500) generates 500 random data points from a standard normal distribution (mean = 0, std = 1).cumfreq(d, numbins=25) computes the cumulative frequency distribution of the data d using 25 bins.r.lowerlimit + np.linspace(0, r.binsize * r.cumcount.size, r.cumcount.size) creates the bin edges corresponding to the cumulative counts.r.cumcount / r.cumcount[-1] normalizes the cumulative counts to get cumulative probabilities (CDF values).Using histogramThis is the most visual and basic method. You create a histogram and visually accumulate the bins to estimate the CDF. While not precise, it's helpful for understanding the concept of cumulative distribution. Python import numpy as np import matplotlib.pyplot as plt d = np.random.randn(500) cnt, b = np.histogram(d, bins=10) p = cnt / cnt.sum() c = np.cumsum(p) fig, a1 = plt.subplots(figsize=(8, 6)) a1.bar(b[:-1], p, width=np.diff(b), color='red', alpha=0.6) a1.set_ylabel('PDF', color='red') a2 = a1.twinx() a2.plot(b[1:], c, color='blue') a2.set_ylabel('CDF', color='blue') plt.title("PDF & CDF via Histogram") plt.show() OutputUsing histogramExplanation:np.random.randn(500) generates 500 random data points from a standard normal distribution.np.histogram(d, bins=10) bins the data into 10 intervals and counts occurrences.cnt / cnt.sum() normalizes counts to get the PDF.np.cumsum(p) calculates the cumulative sum of the PDF to obtain the CDF.a1.bar() plots the PDF and a1.twinx() with a2.plot() plots the CDF on a second y-axis.Read related articles:np.arangescipy.stats.cumfreqhistogramCumulative Distribution FunctionProbability Distribution Function (PDF) Comment More infoAdvertise with us Next Article Calculate and Plot a Cumulative Distribution function with Matplotlib in Python dhruv_tewari Follow Improve Article Tags : Python Python-matplotlib Practice Tags : python Similar Reads How to create a plot of cumulative distribution function in R? Empirical distribution is a non-parametric method used to estimate the cumulative distribution function (CDF) of a random variable. It is particularly useful when you have data and want to make inferences about the population distribution without making any assumptions about its form. In this articl 4 min read How to calculate and plot the derivative of a function using matplotlib and python ? In this article we will plot the derivative of a function using matplotlib and python. Modules used- Matplotlib: Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays. NumPy: It is a python library th 3 min read How to plot a normal distribution with Matplotlib in Python? Normal distribution, also known as the Gaussian distribution, is a fundamental concept in probability theory and statistics. It is a symmetric, bell-shaped curve that describes how data values are distributed around the mean. The probability density function (PDF) of a normal distribution is given b 4 min read Plot Cumulative Distribution Function in R In this article, we will discuss how to plot a cumulative distribution function (CDF) in the R Programming Language. The cumulative distribution function (CDF) of a random variable evaluated at x, is the probability that x will take a value less than or equal to x. To calculate the cumulative distri 4 min read How to calculate probability in a normal distribution given mean and standard deviation in Python? A normal distribution is a type of continuous probability distribution for a real-valued random variable. It is based on mean and standard deviation. The probability distribution function or PDF computes the likelihood of a single point in the distribution. The general formula to calculate PDF for t 2 min read Compute Empirical Cumulative Distribution Function in R The Empirical Cumulative Distribution Function (ECDF) is a non-parametric method for estimating the Cumulative Distribution Function (CDF) of a random variable. Unlike parametric methods, the ECDF makes no assumptions about the underlying probability distribution of the data.It is defined as a step 8 min read Create a cumulative histogram in Matplotlib The histogram is a graphical representation of data. We can represent any kind of numeric data in histogram format. In this article, We are going to see how to create a cumulative histogram in Matplotlib Cumulative frequency: Cumulative frequency analysis is the analysis of the frequency of occurren 2 min read Python - Power-Function Distribution in Statistics scipy.stats.powerlaw() is a power-function continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. Parameters : q : lower and upper tail probability x : quantil 2 min read How to create a Cumulative Histogram in Plotly? Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library 1 min read Cumulative Distribution Function Cumulative Distribution Function (CDF), is a fundamental concept in probability theory and statistics that provides a way to describe the distribution of the random variable. It represents the probability that a random variable takes a value less than or equal to a certain value. The CDF is a non-de 11 min read Like