Skip to content

Instantly share code, notes, and snippets.

@bellbind
Last active April 2, 2018 16:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellbind/bcafacb3b2843e6e0cce46319b021577 to your computer and use it in GitHub Desktop.
Save bellbind/bcafacb3b2843e6e0cce46319b021577 to your computer and use it in GitHub Desktop.
[matplotlib][python3] plot examples
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d # import only for using 3d projection
n = 64
xs = np.linspace(-5, 5, n)
ys = np.linspace(-5, 5, n)
# cone
r, h = 5, 4
zs = np.array([max(h * (1 - np.hypot(x, y) / r), 0)
for x in xs for y in ys]).reshape((n, n))
fig = plt.figure(figsize=(8, 8))
s = fig.add_subplot(111, projection="3d")
mxs, mys = np.meshgrid(xs, ys)
s.plot_surface(mxs, mys, zs)
s.set_aspect("equal")
s.set_xlabel("x")
s.set_ylabel("y")
s.set_zlabel("f(x,y)")
#s.plot_surface(mxs, mys, np.zeros_like(zs))
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tick
fig, (s1, s2) = plt.subplots(1, 2, figsize=(16, 8))
thetas = np.linspace(0, 2*np.pi, 64)
rs = np.ones_like(thetas)
def line_plots(rs):
xs = np.array([r * np.cos(theta) for r, theta in zip(rs, thetas)])
ys = np.array([r * np.sin(theta) for r, theta in zip(rs, thetas)])
s1.plot(xs, ys)
s2.plot(rs, thetas)
pass
line_plots(rs * 1/2)
line_plots(rs * 1)
line_plots(rs * 2)
line_plots(rs * 4)
line_plots(rs * 8)
s1.fill([-10, -10, 10, 10], [-10, 10, 10, -10], alpha=0.25)
s2.fill([0, 0, 10, 10], [0, 2*np.pi, 2*np.pi, 0], alpha=0.25)
def make_grid(s):
s.set_aspect("equal")
s.set_xlim(-10, 10)
s.set_ylim(-10, 10)
# grid
s.xaxis.set_minor_locator(tick.MultipleLocator(1))
s.yaxis.set_minor_locator(tick.MultipleLocator(1))
s.xaxis.set_major_locator(tick.MultipleLocator(2))
s.yaxis.set_major_locator(tick.MultipleLocator(2))
s.grid(True, which="major", color="black", linestyle="-")
s.grid(True, which="minor", color="black", linestyle="--")
pass
make_grid(s1)
make_grid(s2)
s1.set_xlabel("x")
s1.set_ylabel("y")
s1.set_title("(x,y) space")
s2.set_xlabel("r")
s2.set_ylabel("θ")
s2.set_title("(r,θ) space")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.ticker as tick
vs = np.array([[1, 0], [0, 1]])
m = np.array([[2, 1], [2, 3]])
e, v = np.linalg.eig(m)
d, vi = np.diag(e), np.linalg.inv(v)
vs1 = np.vstack([vs, v.T])
#print(vs1)
vs2 = (vi @ vs1.T).T
vs3 = (d @ vi @ vs1.T).T
vs4 = (v @ d @ vi @ vs1.T).T
#vs4 = (m @ vs.T).T
vcs = list(map(colors.hsv_to_rgb, [
(0/3, 3/4, 3/4), (1/3, 3/4, 3/4), (2/3, 3/4, 3/4),
(1/6, 3/4, 3/4), (3/6, 3/4, 3/4), (5/6, 3/4, 3/4)]))
fig, ((s1, s2), (s3, s4)) = plt.subplots(2, 2, figsize=(16, 16))
def area_plot(s, vs):
s.quiver(
np.zeros_like(vs[:,0]), np.zeros_like(vs[:,1]), vs[:,0], vs[:,1],
color=vcs, angles="xy", scale_units="xy", scale=1)
b = np.array([[0, 0], vs[0], vs[0] + vs[1], vs[1]])
e = np.array([[0, 0], vs[2], vs[2] + vs[3], vs[3]])
s.fill(b.T[0], b.T[1], alpha=1/4)
s.fill(e.T[0], e.T[1], alpha=1/4)
pass
area_plot(s1, vs1)
area_plot(s2, vs2)
area_plot(s3, vs3)
area_plot(s4, vs4)
def make_grid(s):
s.set_aspect("equal")
s.set_xlim(-6, 6)
s.set_ylim(-6, 6)
# grid
s.xaxis.set_minor_locator(tick.MultipleLocator(1))
s.yaxis.set_minor_locator(tick.MultipleLocator(1))
s.xaxis.set_major_locator(tick.MultipleLocator(2))
s.yaxis.set_major_locator(tick.MultipleLocator(2))
s.grid(True, which="major", color="black", linestyle="-")
s.grid(True, which="minor", color="black", linestyle="--")
s.set_xlabel("x")
s.set_ylabel("y")
pass
make_grid(s1)
make_grid(s2)
make_grid(s3)
make_grid(s4)
s1.set_title("E")
s2.set_title("V^-1")
s3.set_title("DV^-1")
s4.set_title("A=VDV^-1")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.ticker as tick
vs = np.array([[1, 0], [0, 1], [1,1]])
vcs = list(map(colors.hsv_to_rgb,
[(0/3, 3/4, 3/4), (1/3, 3/4, 3/4), (2/3, 3/4, 3/4)]))
m = np.array([[-2,-1], [2, -2]])
vs2 = (m @ vs.T).T
fig, (s1, s2) = plt.subplots(1, 2, figsize=(16, 8))
s1.quiver(
np.zeros_like(vs[:,0]), np.zeros_like(vs[:,1]), vs[:,0], vs[:,1],
color=vcs,
angles="xy", scale_units="xy", scale=1)
s2.quiver(
np.zeros_like(vs2[:,0]), np.zeros_like(vs2[:,1]), vs2[:,0], vs2[:,1],
color=vcs,
angles="xy", scale_units="xy", scale=1)
def make_grid(s):
s.set_aspect("equal")
s.set_xlim(-5, 5)
s.set_ylim(-5, 5)
# grid
s.xaxis.set_minor_locator(tick.MultipleLocator(1))
s.yaxis.set_minor_locator(tick.MultipleLocator(1))
s.xaxis.set_major_locator(tick.MultipleLocator(5))
s.yaxis.set_major_locator(tick.MultipleLocator(5))
s.grid(True, which="major", color="black", linestyle="-")
s.grid(True, which="minor", color="black", linestyle="--")
s.set_xlabel("x")
s.set_ylabel("y")
pass
make_grid(s1)
make_grid(s2)
s1.set_title("from")
s2.set_title("to: det(A) = 6")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment