Skip to content

Instantly share code, notes, and snippets.

@bee-san
Last active December 12, 2019 03:58
Show Gist options
  • Save bee-san/eff0665b1ac5f99d8f403c75912e9787 to your computer and use it in GitHub Desktop.
Save bee-san/eff0665b1ac5f99d8f403c75912e9787 to your computer and use it in GitHub Desktop.

Made for Skerritt.blog Every new plot starts with the x axis as:

[2, 4, 6, 8, 10, 12, 14]

Then the y axis is that function applied to the x. So log is:

x = [2, 4, 6, 8, 10, 12, 14]
y = [(z * math.log2(z)) for z in x]
import matplotlib.pyplot as plt
import matplotlib.style as style
import math
import numpy as np
style.use('seaborn-poster') #sets the size of the charts
style.use('ggplot')
# constanttime
x = [2, 4, 6, 8, 10, 12, 14]
y = [2] * 7
x = plt.plot(x, y, 'b', label='O(1)')
x[0].set_color('darkgreen')
# log time
x = [2, 4, 6, 8, 10, 12, 14]
y = [2.0, 2.584962500721156, 3.0, 3.321928094887362, 3.584962500721156, 3.807354922057604, 4.0]
x = plt.plot(x, y, 'b', label='O(log n)')
x[0].set_color('blue')
# linear time
x = [2, 4, 6, 8, 10, 12, 14]
y = [2, 4, 6, 8, 10, 12, 14]
x = plt.plot(x, y, 'b', label='O(n)')
x[0].set_color('gold')
x = [2, 4, 6, 8, 10, 12, 14]
y = [(z * math.log2(z)) for z in x]
x = plt.plot(x, y, 'b', label='O(n log n)')
x[0].set_color('goldenrod')
x = [2, 4, 6, 8, 10, 12, 14]
y = [z**2 for z in x]
x = plt.plot(x, y, 'b', label='O(n²)')
x[0].set_color('darkred')
x = [2, 4, 6, 8, 10, 12, 14]
y = [2**z for z in x]
x = plt.plot(x, y, 'b', label='O(2^n)')
x[0].set_color('red')
x = [2, 4, 6, 8, 10, 12, 14]
y = [2, 24, 720, 40320, 3628800, 479001600, 87178291200]
x = plt.plot(x, y, 'b', label='O(n!)')
x[0].set_color('orangered')
plt.xlabel('Elements', size=18)
plt.ylabel('Operations', size=18)
plt.title('Factorial Complexity', size = 24)
plt.legend()
plt.savefig('big_o_factorial_time.png', bbox_inches='tight')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment