다음의 코드들과 그림들은 전자책 파이썬과 함께 하는 선형대수에 수록된 것입니다.
import numpy as np import matplotlib.pyplot as plt import seaborn sns.set_style("darkgrid")
#fig 1.1.1 fig, ax=plt.subplots(figsize=(3,2)) cord=[(0,0), (3,1),(2,3)] nme=["O","A","B"] col=['g','b','r'] for i, j in enumerate(cord): ax.scatter(j[0], j[1], color="white", edgecolors=col[i]) ax.text(j[0], j[1]+0.15, nme[i], color=col[i], fontweight="bold") ax.arrow(0,0, 3, 1, color="b", head_width=0.1) ax.arrow(0,0, 2, 3, color="r", head_width=0.1) ax.text(1, 0.5, r"$\vec{a}$", color="b") ax.text(1, 1.8, r"$\vec{b}$",color="r") ax.spines['left'].set_position(("data", 0)) ax.spines['bottom'].set_position(("data", 0)) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.grid(True) plt.show()
#fig112 a=np.array([10,15]) b=np.array([8,2]) c=np.array([1,2,3]) abSum=a+b abSub=a-b f, ax=plt.subplots(figsize=(4,3)) obj=[a, b, -b, abSum, abSub] nme=['a', 'b', '-b', 'a+b', 'a-b'] col=['g', 'k', 'gray', 'r', 'b'] for i in range(len(obj)): ax.arrow(0, 0, obj[i][0], obj[i][1], color=col[i], head_width=0.5, label=nme[i]) plt.plot([-b[0], abSub[0]], [-b[1], abSub[1]], ls="dashed", color="r") plt.plot([abSub[0], a[0]], [abSub[1], a[1]], ls="dashed", color="r") plt.plot([a[0], abSum[0]], [a[1], abSum[1]], ls="dashed", color="b") plt.plot([abSum[0], b[0]], [abSum[1], b[1]], ls="dashed", color="b") ax.spines['left'].set_position(("data", 0)) ax.spines['bottom'].set_position(("data", 0)) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) plt.legend(loc='best', labelcolor='linecolor') plt.show()
#fig131 x=np.linspace(0.5, 3, 100) plt.figure(figsize=(4,3)) plt.arrow(0,0, 2,1, lw=4, color="brown", head_width=0.05) plt.arrow(0,0, 4,2, color="b", head_width=0.05) plt.arrow(0,0, 1, 3, lw=2, color="r", head_width=0.05) plt.plot(x, -2*x+5, ls="--") plt.text(1.2, 0.9, r"$90^o$", color="g") plt.text(0.2, 0.4, r"$45^o$", color="g") plt.axis("equal") plt.show()
#fig132 plt.figure(figsize=(3,3)) plt.arrow(0,0, 1,0,color="r", lw=2, head_width=0.05) plt.arrow(0,0, -1,0, color="b", lw=2, head_width=0.05) plt.arrow(0,0, 0, 1, color="g", lw=2, head_width=0.05) plt.arrow(-1, 0, 1, 1, ls="-.", color="b",alpha=0.3) plt.arrow(1, 0, -1, 1, ls="-.", color="r",alpha=0.3) plt.xticks([]) plt.yticks([]) plt.text(0.02, 0.02, r"$90^o$", color="r") plt.text(-0.3, 0.02, r"$90^o$", color="b") cor=[(-0.5, -0.1), (0.5, -0.1), (0.1, 0.5), (0.6, 0.5), (-1.1, 0.5)] nme=["-v","v", 'u', "||u-v||", "||u-(-v)||"] col=["r", 'b', 'g', 'r','b'] for i in range(len(cor)): plt.text(cor[i][0], cor[i][1], nme[i], color=col[i], weight="bold") plt.show()
#fig221 x=np.linspace(-8, 8, 100) y1=2-x y2=(-3-2*x)/4 y3=(5-3*x)/6 plt.figure(figsize=(4, 3)) plt.plot(x, y1, color="g", label="y=2-x") plt.plot(x, y2, color="r", label=r"$y=\frac{-3-2x}{4}$") plt.plot(x, y3, color="b", label=r"$y=\frac{5-3x}{6}$") plt.legend(loc="best", labelcolor="linecolor", frameon=False) plt.show()
#fig222 plt.figure(figsize=(3,3)) plt.plot([0, 0] , [0, 3], alpha=0.7, color="brown") plt.plot([0, 3] , [0, 0], alpha=0.7, color="brown") plt.plot([3,3] , [0, 3], alpha=0.7, color="brown") plt.plot([0, 3] , [3, 3], alpha=0.7, color="brown") plt.arrow(0,0, 3, 2, head_width=0.07, color="g") plt.arrow(0,0, 0, 1, head_width=0.07, color="g") plt.arrow(0,1, 3, 2, ls="dashed", lw=0.3, color="g") plt.arrow(3, 2, 0, 1, ls="dashed", color="g") plt.fill_between([0, 3], [0, 2], [1,3], alpha=0.5) nme=["O", "D", 'E', 'C'] cor=[(-0.2, -0.1),(-0.2, 1), (3.1, 3), (3.1, 2)] for i,j in zip(nme, cor): plt.text(j[0], j[1], i, weight="bold", color="g") plt.xticks([0, 1,2,3]) plt.yticks([0,1,2,3]) plt.show()
#fig311 x=np.linspace(-0.5, 6, 100) y1=(7-x)/2 y2=(4+2*x)/5 y3=(-3+5*x)/6 plt.figure(figsize=(4,3)) plt.plot(x,y1, color="g", label=r"$\frac{7-x}{2}$") plt.plot(x, y2, color="b", label=r"$\frac{4+2x}{5}$") plt.plot(x, y3, color="r", label=r"$\frac{-3+5x}{6}$") plt.scatter(3, 2, s=50, c="k") plt.legend(loc="upper center", labelcolor=['g', 'b', 'r'], frameon=False) plt.xticks(np.arange(-0, 6)) plt.yticks(np.arange(-1, 8)) plt.show()
#fig321 O=np.array([0,0]) A1=np.array([1, -1,]) A2=np.array([-2, -1,]) A3=np.array([5, 1]) A4=np.array([-1, 0]) plt.figure(figsize=(3,3)) cor=[A1, A2, A3, A4] nme=['A1', 'A2', 'A3', 'A4'] col=['brown','r','g','orange'] for i in range(len(cor)): plt.arrow(0, 0, cor[i][0], cor[i][1], head_width=0.05, color=col[i], label=nme[i]) plt.plot([0, -0.33*A1[0]], [0,-0.33*A1[1]], color="gray", lw=2, ls="dotted", label="-0.33A1") plt.plot([0, 0.33*A2[0]], [0,0.33*A2[1]], color="k", lw=2, ls="dotted", label="0.33A2") plt.plot([0, -2*A2[0]], [0,-2*A2[1]], color="b", ls="dotted", label="-2A2") plt.plot([0.33*A2[0], A4[0]], [0.33*A2[1], A4[1]], color="gray", ls="dotted", alpha=0.7) plt.plot([-0.33*A1[0], A4[0]], [-0.33*A1[1], A4[1]], color="k", ls="dotted", alpha=0.7) plt.plot([A1[0], A3[0]], [A1[1], A3[1]], color="b", alpha=0.5, ls="dotted") plt.plot([-2*A2[0], A3[0]], [-2*A2[1], A3[1]], color="brown", alpha=0.5, ls="dotted") plt.legend(labelcolor="linecolor", bbox_to_anchor=(1, 1)) plt.show()
#fig341 u, u_sol, Au=[-1,1], [1,2], [-5,-1] v,v_sol, Av=[2,1], [1, 0.5], [4,2] cord=[u, u_sol, Au, v, v_sol, Av] nme=['u', 'u_sol', 'Au', 'v', 'v_sol' , 'Av'] col=['b', 'g', 'skyblue', 'r', 'brown', 'orange'] liw=[1,1,1,2, 3,1] plt.figure(figsize=(3,3)) for i in range(len(cord)): plt.arrow(0,0, cord[i][0], cord[i][1], color=col[i], head_width=0.15, lw=liw[i], label=nme[i]) plt.legend(loc="best", labelcolor="linecolor", frameon=False) plt.show()
#fig411 plt.figure(figsize=(4,3)) x=[(0,3), (0, 5), (0, 7)] y=[(2, 1), (2, 3), (2, 5), (2, 7)] xnme=['x1', 'x2', 'x3'] ynme=['y1', 'y2', 'y3', 'y4'] plt.scatter([0,0,0], [3, 5, 7], s=50, c="b") plt.scatter([2, 2,2,2], [1, 3, 5, 7], s=50, c="orange") plt.scatter([2,2,2], [3, 5, 7], s=50, c="brown") for i in range(3): plt.arrow(x[i][0], x[i][1], 1.8, 0, head_width=0.1, color="g") plt.text(x[i][0], x[i][1]+0.3, xnme[i], color="b", weight="bold") plt.text(y[i+1][0], y[i+1][1]+0.3, ynme[i], color="brown", weight="bold") plt.text(y[0][0], y[0][1]+0.3, ynme[3], color="orange", weight="bold") plt.text(0.7, 7.3, "T(=Function)", weight="bold", color="g") plt.xlim(-0.5, 3) plt.ylim(-0.5, 9) plt.text(-0.3, 0, "(x1,x2,x3): Domain\n(y1, y2, y3, y4): Codomain\n(y1, y2, y3): Range") plt.xticks([]) plt.yticks([]) plt.show()
#fig412 x=[(0, 1), (0,3), (0, 5), (0, 7), (0, 9)] y=[(2, 1), (2, 3), (2, 5), (2, 7)] xnme=['x1', 'x2', 'x3', 'x4', 'x5'] ynme=['y1', 'y2', 'y3', 'y4'] f, ax=plt.subplots(1, 3, figsize=(10,3)) ax[0].scatter([0,0,0], [3, 5, 7], s=50, c="b") ax[0].scatter([2, 2,2,2], [1, 3, 5, 7], s=50, c="brown") for i in range(1,4): ax[0].arrow(x[i][0], x[i][1], 1.8, 0, head_width=0.1, color="g") ax[0].set_xticks([]) ax[0].set_yticks([]) ax[0].set_xlim(-0.5, 2.5) ax[0].set_ylim(-0.5, 8) ax[0].set_xlabel("(a)", weight="bold", size=12) ax[1].scatter([0,0,0,0,0], [1, 3, 5, 7, 9], s=50, c="b") ax[1].scatter([2, 2,2,2], [1, 3, 5, 7], s=50, c="brown") for i in range(4): ax[1].arrow(x[i][0], x[i][1], 1.8, 0, head_width=0.1, color="g") ax[1].arrow(x[4][0], x[4][1], 1.8, -1.8, head_width=0.1, color="g") ax[1].arrow(x[1][0], x[1][1], 1.8, 1.8, head_width=0.1, color="g") ax[1].set_xticks([]) ax[1].set_yticks([]) ax[1].set_xlim(-0.5, 2.5) ax[1].set_ylim(-0.5, 9.5) ax[1].set_xlabel("(b)", weight="bold", size=12) ax[2].scatter([0,0,0,0], [1, 3, 5, 7], s=50, c="b") ax[2].scatter([2, 2,2,2], [1, 3, 5, 7], s=50, c="brown") for i in range(4): ax[2].arrow(x[i][0], x[i][1], 1.8, 0, head_width=0.1, color="g") ax[2].set_xticks([]) ax[2].set_yticks([]) ax[2].set_xlim(-0.5, 2.5) ax[2].set_ylim(-0.5, 7.5) ax[2].set_xlabel("(c)", weight="bold", size=12) plt.show()
#fig431 u, v=np.array([3,0]), np.array([0, 3]) plt.figure(figsize=(3,3)) cord=[u, v, -u, v-u, v-(-u)] nme=['u', 'v', '-u', 'v-u', 'v-(-u)'] col=['b', 'r', 'navy', 'brown', 'orange'] for i in range(len(nme)): plt.arrow(0,0, cord[i][0], cord[i][1], color=col[i], head_width=0.1, label=nme[i]) plt.text(cord[i][0]+0.1, cord[i][1]+0.1, nme[i], weight="bold", size=12, color=col[i]) #plt.legend( labelcolor="linecolor", bbox_to_anchor=(1,1)) plt.text(0.3, 0.1, r"$90^o$", color="b") plt.text(-1, 0.1, r"$90^o$", color="navy") plt.show()
#fig433 x=np.linspace(0.5, 3, 100) plt.figure(figsize=(4,3)) plt.arrow(0,0, 4,2, color="r", head_width=0.1) plt.arrow(0,0, 2,1, lw=4, color="b",alpha=0.5, head_width=0.1) plt.arrow(0,0, 1, 3, lw=2, color="b", head_width=0.1) plt.plot(x, -2*x+5, ls="--") plt.text(1.2, 0.9, r"$90^o$", color="b") plt.text(0.2, 0.4, r"$\theta$", color="g") cord=[(1, 0.2), (1,3.2), (3.2, 1.8)] nme=[r'$b_{proj}$', 'b', 'a'] col=['b', 'b', 'r'] for i in range(3): plt.text(cord[i][0], cord[i][1], nme[i], color=col[i], weight='bold', size=11) plt.axhline(y=0, color='k') plt.axvline(x=0, color='k') plt.xticks([]) plt.yticks([]) plt.axis("equal") plt.show()
#fig434 a,b=np.array([1,0]), np.array([2,1]) b_proj=np.array([2,0]) cord=[a, b, b_proj] nme=['a', 'b', 'b_proj'] col=['r','b','gray'] llw=[2, 1, 1] plt.figure(figsize=(3,3)) for i in range(len(nme)): plt.arrow(0, 0, cord[i][0], cord[i][1], head_width=0.05, lw=llw[i],color=col[i]) plt.text(cord[i][0]+0.05, cord[i][1]+0.05, nme[i], color=col[i], weight="bold", size=11) plt.vlines(2, 0, b[1], ls="dotted", color="b", alpha=0.5) plt.text(1.7, 0.03, r"$90^o$", color="b") plt.show()
#fig435 u, v, z, au=np.array([1,0]),np.array([0,2]), np.array([2,2]), np.array([2,0]) cord=[u, v, z, au] nme=['u', r'v = z-$\alpha$u', r'z = v+$\alpha$u', r'$\alpha$u'] col=['r','b', 'g', 'brown'] llw=[2, 1, 1, 1] plt.figure(figsize=(3,3)) for i in range(len(nme)): plt.arrow(0, 0, cord[i][0], cord[i][1], head_width=0.05, lw=llw[i],color=col[i]) plt.text(cord[i][0]+0.05, cord[i][1]+0.05, nme[i], color=col[i], weight="bold", size=11) plt.vlines(2, 0, z[1], ls="dashed", color="b", alpha=0.5) plt.hlines(2, 0, au[0], ls="dashed", color="brown", alpha=0.5) plt.text(1.7, 0.03, r"$90^o$", color="b") plt.show()
#fig436 z, u, v=np.array([7,6]), np.array([4, 2]), np.array([-1,2]) u_proj=2*u cord=[z,u,v, u_proj] nme=['z','u','v', 'u_proj'] col=['r','b', 'g', 'gray'] llw=[1, 3, 1, 1] plt.figure(figsize=(3,3)) for i in range(len(nme)): plt.arrow(0, 0, cord[i][0], cord[i][1], head_width=0.1, lw=llw[i],color=col[i]) plt.text(cord[i][0]+0.3, cord[i][1]+0.3, nme[i], color=col[i], weight="bold", size=11) plt.plot([v[0], z[0]], [v[1], z[1]], ls="dashed", color="gray", alpha=0.7) plt.plot([z[0], u_proj[0]], [z[1], u_proj[1]], ls="dashed", color="g", alpha=0.7) plt.text(0.1, 0.5, r"$90^o$", color="b") plt.axis("equal") plt.show()
#fig437 y,u, y_p, y_pOrtho=np.array([-5,3]), np.array([3,3]), np.array([-1,-1]), np.array([-4,4]) cord=[y,u, y_p, y_pOrtho] nme=['y','u', 'y_p', 'y_pOrtho'] col=['r','b', 'g', 'gray'] plt.figure(figsize=(3,3)) for i in range(len(nme)): plt.arrow(0, 0, cord[i][0], cord[i][1], head_width=0.1,color=col[i], label=nme[i]) plt.plot([y[0], y_p[0]], [y[1], y_p[1]], ls="dashed", color="gray", alpha=0.7) plt.plot([y[0], y_pOrtho[0]], [y[1], y_pOrtho[1]], ls="dashed", color="g", alpha=0.7) plt.text(-1.2, -0.3, r"$90^o$", color="b") plt.legend(labelcolor="linecolor", loc=(0.4, 0.6), frameon=False) plt.axis("equal") plt.show()
댓글
댓글 쓰기