'How do I fill in the gap between multiple CDF plots in python?
I am plotting multiple CDF plots in python using this code:
count, bins_count = np.histogram(np.log10(my_array), bins=100)
#Finding the pdf for the histogram
pdf = count / sum(count)
#Calculating the cdf
cdf = np.cumsum(pdf)
plt.plot(bins_count[1:], cdf, color='g')
I am repeating this multiple times in a loop and putting them all on a single graph. I need to shade in the region between that these CDF plots span. How would I go about doing this?
I am using matplotlib to plot the graphs.
If anyone needs any more classification please let me know.
Thanks in advance for any help.
Solution 1:[1]
One could draw all the curves on top of each other, using some alpha value. That will generate something resembling a fill; the spots where more curves coincide will be darker.
from matplotlib import pyplot as plt
import numpy as np
for _ in range(100):
data = 10 ** np.random.randn(10, 1000).cumsum(axis=1).ravel()
count, bin_edges = np.histogram(np.log10(data), bins=100)
# Finding the pdf for the histogram
pdf = count / sum(count)
# Calculating the cdf
cdf = np.cumsum(pdf)
plt.plot(bin_edges[1:], cdf, color='g', alpha=0.1)
Another approach uses fill_between. This needs a common x-axis. Therefore, the example code below works with fixed bins and calculates the overall minimum and maximum curves.
min_curve = 1
max_curve = 0
for _ in range(100):
bin_edges = np.linspace(-80, 80, 500)
data = 10 ** np.random.randn(10, 1000).cumsum(axis=1).ravel()
count, _ = np.histogram(np.log10(data), bins=bin_edges)
# Finding the pdf for the histogram
pdf = count / sum(count)
# Calculating the cdf
cdf = np.cumsum(pdf)
min_curve = np.minimum(min_curve, cdf)
max_curve = np.maximum(max_curve, cdf)
plt.fill_between(bin_edges[1:], min_curve, max_curve, color='g', alpha=0.3)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JohanC |

