None
import matplotlib.pyplot as plt
import numpy as np
import itertools
# problem 1
def f(x):
return 1/(x-1)
x1 = np.linspace(-2, 0.99)
x2 = np.linspace(1.01, 6)
y1 = f(x1)
y2 = f(x2)
plt.plot(x1, y1, lw=4, ls='--', c="m")
plt.plot(x2, y2, lw=4, ls='--', c="m")
plt.xlim(-2, 6)
plt.ylim(-6, 6)
plt.show()
# problem 2
x = np.linspace(0, np.pi*2)
styles = [
{"c": "g", "ls": "-"},
{"c": "r", "ls": "--"},
{"c": "b", "ls": "--"},
{"c": "m", "ls": ":"}
]
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax1.set_title("sin(x)")
ax1.plot(x, np.sin(x), **styles[0])
plt.axis([0, np.pi*2, -2, 2])
ax2 = fig.add_subplot(222)
ax2.set_title("sin(2x)")
ax2.plot(x, np.sin(2*x), **styles[1])
plt.axis([0, np.pi*2, -2, 2])
ax3 = fig.add_subplot(223)
ax3.set_title("2sin(x)")
ax3.plot(x, 2*np.sin(x), **styles[2])
plt.axis([0, np.pi*2, -2, 2])
ax4 = fig.add_subplot(224)
ax4.set_title("2sin(2x)")
ax4.plot(x, 2*np.sin(2*x), **styles[3])
plt.axis([0, np.pi*2, -2, 2])
plt.suptitle("Bunch of plots")
plt.show()
# problem 2 - alternative
x = np.linspace(0, np.pi*2)
y = [np.sin(x), 2*np.sin(x), np.sin(2*x), 2*np.sin(2*x)]
titles = ["sin(x)", "2sin(x)", "sin(2x)", "2sin(2)"]
styles = [
{"c": "g", "ls": "-"},
{"c": "b", "ls": "--"},
{"c": "r", "ls": "--"},
{"c": "m", "ls": ":"}
]
fig, axs = plt.subplots(2, 2)
fig.suptitle("Bunch of plots")
q = itertools.chain.from_iterable(zip(*axs)) # goes by column rather than row
for ax, f, titl, style in zip(q, y, titles, styles):
ax.plot(x, f, **style)
ax.axis([0, np.pi*2, -2, 2])
ax.set_title(titl)
# problem 3
y = np.loadtxt("results.txt", delimiter=",", skiprows=1)
x = np.arange(1, 22)
fig = plt.figure(figsize=(7,7))
ax1 = fig.add_subplot(221)
ax1.plot(x, y[:,0])
ax1.set_title("asg1 impl")
ax2 = fig.add_subplot(222)
ax2.loglog(x, y[:,0])
ax2.set_title("asg1 log") # polynomial
ax3 = fig.add_subplot(223)
ax3.plot(x, y[:,1])
ax3.set_title("numpy impl")
ax4 = fig.add_subplot(224)
ax4.semilogy(x, y[:,1])
ax4.set_title("numpy log-linear") # exponential