%matplotlib inline import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 0.5, -0.5, -1, -0.5, 0.5, 1],[0, 0.866, 0.866, 0, -0.866, -0.866, 0]) ax.plot([0,1],[0,0]) ax.plot([0,0.5],[0,0.866]) ax.plot([0,-.5],[0,0.866]) ax.plot([0,-1],[0,0]) ax.plot([0,-.5],[0,-0.866]) ax.plot([0,0.5],[0,-0.866]) ax.set_title('Title') ax.set_xlabel('horizontal-x') ax.set_ylabel('vertical-y') ax.text(-0.15,0.5,'triangle') ax.set_aspect('equal') fig.tight_layout() fig.savefig('nice_fig.png')
%matplotlib inline import matplotlib.pyplot as plt import numpy as np plt.rc('font', size=18) fig, ax = plt.subplots() ax.set_aspect('equal') ax.grid() thetas = np.linspace(-np.pi/2,np.pi/4) x1 = np.cos(thetas) y1 = np.sin(thetas) x2 = [(1+np.sqrt(2)),0] y2 = [-1, -1] xs = np.concatenate((x1,x2)) ys = np.concatenate((y1,y2)) ax.fill(xs,ys,linewidth=3,edgecolor='k') ax.fill(ys,xs,linewidth=3,edgecolor='k') thetas = np.linspace(np.pi,np.pi*3/2) x1 = np.cos(thetas) y1 = np.sin(thetas) x2 = [-1] y2 = [-1] xt = np.concatenate((x1,x2)) yt = np.concatenate((y1,y2)) ax.fill(xt,yt,linewidth=3,edgecolor='k') ax.arrow((1+np.sqrt(2)),0.2,0,-1.15,width=.05,length_includes_head=True,facecolor='w') ax.text(2,0.5,'$x=1+\sqrt{2}$') ax.set_xlabel('$x$') ax.set_ylabel('$y$') fig.tight_layout() fig.savefig('jupyter21_exercise3_triangular_hole_01.png')
fig, ax = plt.subplots() ax.set_aspect('equal') ax.grid() ax.fill(xs+1,ys,linewidth=3,edgecolor='k') ax.fill(ys,xs+1,linewidth=3,edgecolor='k') ax.fill(xt+1,yt+1,linewidth=3,edgecolor='k') ax.set_xlabel('$x$') ax.set_ylabel('$y$') fig.tight_layout() fig.savefig('jupyter21_exercise3_triangular_hole_02.png')
for
-løkker:
%matplotlib inline import matplotlib.pyplot as plt import numpy as np plt.rc('font', size=18) thetas = np.linspace(np.pi,np.pi/2) xs = 2 * np.cos(thetas) + 2 ys = 2 * np.sin(thetas) + 0 thetas = np.linspace(np.pi/2,np.pi) x1 = np.cos(thetas) + 2 y1 = np.sin(thetas) + 1 xs = np.concatenate((xs,x1)) ys = np.concatenate((ys,y1)) thetas = np.linspace(-np.pi/2,0) x1 = 2 * np.cos(thetas) + 1 y1 = 2 * np.sin(thetas) + 3 xs = np.concatenate((xs,x1)) ys = np.concatenate((ys,y1)) thetas = np.linspace(0,-np.pi/2) x1 = 3 * np.cos(thetas) + 0 y1 = 3 * np.sin(thetas) + 3 xs = np.concatenate((xs,x1)) ys = np.concatenate((ys,y1)) fig, ax = plt.subplots() ax.set_aspect('equal') ax.grid() ax.set_yticks([0,1,2,3]) ax.fill(xs,ys,linewidth=3,edgecolor='k') ax.set_xlabel('$x$') ax.set_ylabel('$y$') fig.tight_layout() fig.savefig('jupyter21_exercise_claw.png')
for
-løkker:
%matplotlib inline import matplotlib.pyplot as plt import numpy as np plt.rc('font', size=18) ts = [(np.pi,np.pi/2), (np.pi/2,np.pi), (-np.pi/2,0), (0,-np.pi/2)] cs = [(2,0),(2,1),(1,3),(0,3)] rs = [2, 1, 2, 3] xs = [] ys = [] for (t1,t2),(cx,cy),r in zip(ts,cs,rs): thetas = np.linspace(t1,t2) x1 = r * np.cos(thetas) + cx y1 = r * np.sin(thetas) + cy xs = np.concatenate((xs,x1)) ys = np.concatenate((ys,y1)) fig, ax = plt.subplots() ax.set_aspect('equal') ax.grid() ax.set_yticks([0,1,2,3]) ax.fill(xs,ys,linewidth=3,edgecolor='k') ax.set_xlabel('$x$') ax.set_ylabel('$y$') fig.tight_layout() fig.savefig('jupyter21_exercise_claw.png')
%matplotlib inline import matplotlib.pyplot as plt import numpy as np plt.rc('font', size=18) fig, axs = plt.subplots(3,1,sharex=True,figsize=(6,10)) fig.suptitle('Smooth cutoff functions') Rs = [2, 4, 6] gammas = [1, 2, 3] for ax,R in zip(axs,Rs): rs1 = np.linspace(0,R,100) rs2 = np.linspace(R,10,100) rs = np.concatenate((rs1,rs2)) ys2 = np.zeros(100) for gamma in gammas: f = lambda r: 1 + gamma * np.power(r/R,gamma + 1) - (gamma + 1) * np.power(r/R,gamma) ys1 = f(rs1) ys = np.concatenate((ys1,ys2)) ax.plot(rs,ys) ax.grid() ax.legend(['$\gamma={}$'.format(g) for g in gammas]) ax.set_ylabel('$y$') ax.set_title('$R={}$'.format(R)) ax.set_xlabel('$r$') fig.tight_layout() fig.subplots_adjust(top=0.9) fig.savefig('loesning_funktioner_01.png')
%matplotlib inline import numpy as np import matplotlib.pyplot as plt plt.rc('font', size=16) fig, ax = plt.subplots() ax.set_aspect('equal') ax.grid() ax.set_xlim([-1,7]) ax.set_ylim([-1,4]) ax.set_xticks(range(7)) ax.set_yticks(range(4)) xs = np.array([0, 1, 1, 0]) ys = np.array([0, 0, 1, 1]) def plot_square_more_compact(x,y,col): return ax.fill(x + xs, y + ys, linewidth=3, facecolor=col, edgecolor='k')[0] # note the [0] my_square = plot_square_more_compact(1,0,'pink') fig.savefig('pyth21_manipulating_square_01.png')
def move_square(square,dx,dy,col): square.set(facecolor=col) xy_vals = square.get_xy() new_xy_vals = xy_vals + [dx,dy] square.update({'xy': new_xy_vals}) move_square(my_square,1,0,'pink') fig.savefig('pyth21_manipulating_square_02.png') fig
p = 3 dxs = (4 * [1] + 4 * [-1]) * p dys = [0] * len(dxs) cols = (3 * ['pink'] + ['white']) * 2 * p
# move back to start move_square(my_square,-1,0,'pink') i = 0 for dx, dy, col in zip(dxs,dys,cols): move_square(my_square,dx,0,col) fig.savefig('pyth21_manipulating_square_{:02d}.png'.format(i)) i += 1
%matplotlib inline import numpy as np import matplotlib.pyplot as plt np.set_printoptions(precision=3) plt.rc('font', size=16) fig, ax = plt.subplots() ax.grid() ax.set_xlim(-4,4) f = lambda x0,x: np.exp(-np.power(x-x0,2)) xs = np.linspace(-5,5,100) ys = f(-2,xs) gauss = ax.plot(xs,ys)[0] fig.savefig('pyth21_moving_gauss_01.png')
from matplotlib import animation plt.rc('animation', html='jshtml') N = 50 x0s = np.linspace(-2,2,N) def update(i): x0 = x0s[i] ys = f(x0,xs) gauss.set_data((xs,ys)) return [gauss] anim = animation.FuncAnimation(fig, update, frames=N, interval=10, blit=True) anim
writer = animation.writers['pillow'](fps=10) # frames per second anim.save('moving_gauss.gif', writer=writer)
import numpy as np import matplotlib.pyplot as plt plt.rc('font', size=16) fig, ax = plt.subplots() ax.set_aspect('equal') ax.grid() # ramp N =44 xramp = np.linspace(0,10,N) yramp = (10 - xramp) * 0.2 xramp = np.concatenate(([0],xramp)) yramp = np.concatenate(([0],yramp)) ax.fill(xramp,yramp,linewidth=3,facecolor='darkorange',edgecolor='k') # box v1 = np.array([10,-2])/np.sqrt(10**2 + (-2)**2) v2 = [-v1[1],v1[0]] xs = [0, v1[0], v1[0]+v2[0], v2[0]] ys = [0, v1[1], v1[1]+v2[1], v2[1]] ys = np.array(ys) + 2 box = ax.fill(xs,ys,linewidth=3,facecolor='steelblue',edgecolor='k')[0] fig.savefig('pyth21_sliding_box_01.png')
from matplotlib import animation plt.rc('animation', html='jshtml') def update(i): xy = box.get_xy() box.update({'xy': xy + v1/5}) return [box] anim = animation.FuncAnimation(fig, update, frames=N, interval=10, blit=True) anim
Choose which booklet to go to: