Skip to content

Instantly share code, notes, and snippets.

@jzhou77
Created October 7, 2020 20:50
Show Gist options
  • Save jzhou77/fd46d521e6321bd34f4fc9c01e32d32c to your computer and use it in GitHub Desktop.
Save jzhou77/fd46d521e6321bd34f4fc9c01e32d32c to your computer and use it in GitHub Desktop.
CDF Plot
'recovery-duration.txt' file content:
Duration
0.127489
0.134138
0.151415
0.180696
0.196397
0.212269
0.243426
0.263445
0.272921
0.274783
0.308237
Script to generate CDF plot:
from pandas import read_csv, to_datetime
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
df = read_csv('recovery-duration.txt')
stats_df = df.groupby('Duration')['Duration'].agg('count').pipe(pd.DataFrame)\
.rename(columns = {'Duration': 'frequency'})
# PDF
stats_df['pdf'] = stats_df['frequency'] / sum(stats_df['frequency'])
# CDF
stats_df['cdf'] = stats_df['pdf'].cumsum()
stats_df = stats_df.reset_index()
# stats_df
fig = plt.figure()
# stats_df.plot(x='Duration', y=['cdf'], grid = True)
plt.plot('Duration', 'cdf', data=stats_df, linewidth=2, )
plt.xlabel('Recovery Duration (s)')
plt.ylabel('CDF')
plt.grid(True)
plt.ylim(0.0, 1.0)
plt.xlim(0, 10)
SMALL_SIZE = 10
MEDIUM_SIZE = 12
BIGGER_SIZE = 14
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
fig.savefig('recovery-duration.pdf')
fig.savefig('recovery-duration.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment