Created
February 19, 2026 13:20
-
-
Save theeluwin/27209a9a5197329670b24d944cfad5b3 to your computer and use it in GitHub Desktop.
plotting various growth functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| import matplotlib.pyplot as plt | |
| plt.rcParams.update({ | |
| 'font.family': 'Times New Roman', | |
| 'mathtext.fontset': 'stix', | |
| }) | |
| plt.figure() | |
| plt.ylim(0, 10000) | |
| plt.xlabel(r'$n$') | |
| plt.ylabel(r'$f(n)$') | |
| plt.grid(True) | |
| colors = ['red', 'orange', 'gold', 'green', 'blue', 'navy', 'purple'] | |
| fs = [ | |
| (lambda x: 1, r'$1$'), | |
| (lambda x: math.log(x, 2), r'$\log n$'), | |
| (lambda x: x, r'$n$'), | |
| (lambda x: x * math.log(x, 2), r'$n\log n$'), | |
| (lambda x: x * x, r'$n^2$'), | |
| (lambda x: x * x * x, r'$n^3$'), | |
| (lambda x: 2 ** x, r'$2^n$'), | |
| ] | |
| X = list(range(1, 21)) | |
| for (f, label), c in zip(fs, colors): | |
| Y = [f(x) for x in X] | |
| plt.plot(X, Y, marker='o', label=label, color=c) | |
| plt.xticks(X) | |
| plt.legend() | |
| plt.savefig('plot.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment