Code
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
= [np.power(10, k) for k in np.arange(1, 2.75, 0.25)]
n_vals = pd.DataFrame({'$n$': n_vals})
runtime_df '$n^2 + 50n$'] = runtime_df['$n$'].apply(lambda x: np.power(x, 2) + 50*x)
runtime_df['$n^2 + 10000$'] = runtime_df['$n$'].apply(lambda x: np.power(x, 2) + 10000)
runtime_df['$O(n)$'] = runtime_df['$n$'].copy()
runtime_df['$O(nlogn)$'] = runtime_df['$n$'].apply(lambda x: x * np.log(x))
runtime_df['$O(n^2)$'] = runtime_df['$n$'].apply(lambda x: np.power(x, 2))
runtime_df['$O(n^2logn)$'] = runtime_df['$n$'].apply(lambda x: np.power(x,2) * np.log(x))
runtime_df['$O(n^3)$'] = runtime_df['$n$'].apply(lambda x: np.power(x, 3))
runtime_df['$O(n^3logn)$'] = runtime_df['$n$'].apply(lambda x: np.power(x, 3) * np.log(x))
runtime_df[# Get the max values, for labeling the ends of lines
= runtime_df.max().to_dict()
max_vals = runtime_df.melt(id_vars=['$n$'])
plot_df #print(plot_df)
= {col: '' if (col == '$n^2 + 50n$') or (col == '$n^2 + 10000$') else (2,1) for col in runtime_df.columns}
style_map = plt.subplots(figsize=(11,5))
fig, ax ='$n$', y='value', hue='variable', style='variable', dashes=style_map)
sns.lineplot(plot_df, x#plt.xscale('log')
'log')
plt.yscale(# extract the existing handles and labels
= ax.get_legend_handles_labels()
h, l # slice the appropriate section of l and h to include in the legend
0:2], l[0:2])
ax.legend(h[for label, val in max_vals.items():
if (label == '$n$') or (label == '$n^2 + 50n$') or (label == '$n^2 + 10000$'):
continue
if 'logn' in label:
= label.replace('logn', r'\log(n)')
label = max_vals['$n$'] + 2, y = val, s=label, va='center')
ax.text(x # Hide the right and top spines
'right', 'top']].set_visible(False)
ax.spines[[ plt.show()