# -*- coding: utf-8 -*- """ Created on Tue Mar 10 08:01:50 2020 @author: Johan Rene van Dorp """ import numpy as np import matplotlib.pyplot as plt data = np.array([4,3,9,1,7]) data.sort() n_data = len(data) ecdf = np.arange(1,n_data+1)/n_data off_set_x = 2 data = np.append(min(data)-off_set_x,data) data = np.append(data,max(data)+off_set_x) ecdf = np.append(0,ecdf) ecdf = np.append(ecdf,1) # Creating the cumulative distribution figure plt.rc('font', family='serif', size='10') plt.figure(figsize=(10, 5)) CDF_figure = plt.figure() # Adding the left panel with the theoretical pmf Panel = CDF_figure.add_subplot(1,1,1) Panel.axhline(1, lw=1, ls = ':',color = 'darkgray',alpha = 0.5) plt.xlim((-0.5,10.5)) plt.ylim((-0.05,1.1)) plt.xlabel('x') plt.ylabel('Empirical CDF $ \hat{F}(x) = Pr(X \leq x)$') plt.title('Empirical CDF Example') plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) text_str = 'n = '+ str(n_data) plt.text(4.75,0.9,text_str,color = 'red') the_col = 'IndianRed' x = data y = ecdf closed_x = x closed_y = y open_x = x[1:len(x)-1] open_y = y[0:len(y)-2] Panel.scatter(closed_x,closed_y,color = the_col) Panel.scatter(open_x,open_y,edgecolor = the_col,facecolor = 'white') Panel.vlines(open_x, open_y, closed_y[1:len(closed_y)-1], color='black', linestyles=':', lw=1) Panel.hlines(closed_y[0:len(closed_y)-1], closed_x[0:len(closed_x)-1], closed_x[1:len(closed_x)], linestyles='-', lw=2,color = the_col) plt.savefig('ECDF_Example.png', dpi=1200)