다음 그림들은 전자책 파이썬과 함께하는 미분적분의 7장에 수록된 그래프들과 코드들입니다.
import numpy as np import pandas as pd from sympy import * import matplotlib.pyplot as plt import seaborn as sns sns.set_style("darkgrid")
def axisTran(ax): 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)
#그림 7.1.1 a=symbols("a") f=(a-1)*(a-2)*(a-5)+5 x=np.linspace(0.5,5, 100) y=[f.subs(a, i) for i in x] fig,ax=plt.subplots(figsize=(4,3)) ax.plot(x, y ,color="g", label="f(x)") idx=np.linspace(0, 99, 7).astype(int) for i in range(1, len(idx)): x0=x[[(idx[i-1]),idx[i]]] y0=[f.subs(a, i) for i in x0] ax.scatter(x0, y0, s=50, c="b") ax.plot(x0, y0, ls="dashed", color="r") ax.text(x[(idx[i-1])]+0.2, f.subs(a, x[(idx[i-1])]), f"p{i-1}" , color="b") ax.text(x[(idx[6])], f.subs(a, x[(idx[6])])+0.5, "p6" , color="b") ax.vlines(0.5, f.subs(a, 0.5), 0,ls="dashed", alpha=0.7, color="gray") ax.vlines(5, 0, float(f.subs(a, 5)), ls="dashed", alpha=0.7, color="gray") axisTran(ax) ax.set_xlabel("x", fontsize=11) ax.set_xticks([0.5, 5], ["a", "b"]) ax.set_yticks([]) ax.set_ylabel("f(x)", rotation="horizontal", labelpad=10, fontsize=11) ax.legend(loc=(0.6, 0.8)) plt.show()
#그림 7.2.1 x=np.linspace(0, 1, 100) f=np.sqrt(x) g=-f plt.figure(figsize=(4,3)) plt.fill_between(x, f , color="g", alpha=0.5, label="f(x)") plt.fill_between(x, g, color="r", alpha=0.3, label="g(x)=-f(x)") plt.vlines(1, 0, 1, color="b", ls="--",lw=2) plt.text(1.05, 0.5, "radius\nof body") plt.xlabel("x", fontsize=11) plt.xlim((0, 1.2)) plt.ylabel("y", rotation="horizontal", labelpad=10, fontsize=11) plt.legend(loc=(0.05, 0.8), frameon=False) plt.show()
#그림 7.2.5 x=np.linspace(-1, 4, 100) f=x**2-2*x g=x plt.figure(figsize=(4,3)) plt.plot(x, f , color="g", label=r"$f(x)=x^2-2x$") plt.plot(x, g, color="b", label="g(x)=x") x0=np.linspace(0, 3, 50) plt.fill_between(x0, x0**2-2*x0, x0, color="brown", alpha=0.3) plt.hlines(4, -1, 4, color="r", ls="--", label="axis of rotation") plt.vlines(1, 1, 4, color="k") plt.text(0.7, 2.2, r"$r_2$", fontsize=11) plt.vlines(1.5, 1.5**2-2*1.5, 4, color="k") plt.text(1.6, 2.2, r"$r_1$", fontsize=11) plt.xlabel("x", fontsize=11) plt.ylabel("y", rotation="horizontal", labelpad=10, fontsize=11) plt.legend(loc=(0.3, 0.7), labelcolor=["g" ,"b", "r"], frameon=False) plt.show()
#그림 7.3.1 tab=pd.Series([7/20, 6/20, 4/20, 3/20], index=['Black',"Yellow",'Green', 'Red']) plt.figure(figsize=(4,3)) plt.bar(["Black", "Yellow","Green","Red"], tab, color="brown", alpha=0.5) plt.xticks(color="brown") plt.ylabel("probability", rotation="horizontal", labelpad=30) plt.show()
#그림 7.3.2 x1, x2=np.linspace(2, 4, 60), np.linspace(4, 5, 20) y1, y2=np.repeat(1/3, 60), np.repeat(1/3, 20) fig,ax=plt.subplots(figsize=(4,3)) ax.fill_between(x1, y1, color="b", alpha=0.3, label=r"Area=$\frac{2}{3}$ A") ax.fill_between(x2, y2, color="r", alpha=0.3, label=r"Area=$\frac{1}{3}$ A") axisTran(ax) ax.set_xlabel("x", fontsize=11) ax.set_xlim((0, 5.3)) ax.set_ylabel("Probability", labelpad=10, fontsize=11) ax.legend(loc=(0.01, 0.7), labelcolor=["b", "r"], frameon=False) plt.show()
#그림 7.3.3 x=np.linspace(0,3, 100) f=2*np.exp(-2*x) fig,ax=plt.subplots(figsize=(4,3)) ax.fill_between(x, f, color="g", alpha=0.3, label="f(x)=2exp(-2x)") ax.text(0.1, 0.3, "area=1", color="k") axisTran(ax) ax.set_xlabel("x", fontsize=11) ax.set_ylabel("f(x)", rotation="horizontal",labelpad=10, fontsize=11) ax.legend(loc=(0.4, 0.3), labelcolor="g", frameon=False) plt.show()
#그림 7.3.4 x=np.linspace(-2,2, 100) g=np.exp(-x**2) plt.figure(figsize=(4,3)) plt.fill_between(x, g, color="g", alpha=0.3, label=r"$g(x)=2exp(-x^2)$") plt.xlabel("x", fontsize=11) plt.ylabel("g(x)", rotation="horizontal",labelpad=10, fontsize=11) plt.legend(loc=(0.6, 0.9), labelcolor="g", frameon=False) plt.show()
#그림 7.3.5 x=np.linspace(-3,3, 100) f=np.exp(-x**2/2)/np.sqrt(2*np.pi) plt.figure(figsize=(4,3)) plt.fill_between(x, f, color="g", alpha=0.3, label=r"$f(x)=\frac{1}{2\pi}exp(-\frac{x^2}{2})$") plt.text(0.1, 0.02, r"$\int^\infty_{-\infty}f(x)=1(area)$") plt.xlabel("x", fontsize=11) plt.ylabel("f(x)", rotation="horizontal",labelpad=10, fontsize=11) plt.legend(loc=(0.6, 0.9), labelcolor="g", frameon=False) plt.show()
#그림 7.3.6 a, mu, s=symbols("a, mu, s") f=1/(s*sqrt(2*pi))*exp(-1/2*((a-mu)/s)**2) x=np.linspace(-6, 6, 100) fig,(ax1, ax2)=plt.subplots(nrows=1, ncols=2, figsize=(8,3)) col=["g","b","r"] for i,j in enumerate([-2, 0, 2]): y=[f.subs({a:k, mu:j, s:1}) for k in x] ax1.plot(x, y, color=col[i], label=f"N({j}, 1)") ax1.set_xlabel("x\n(a)", fontsize=11) ax1.set_ylabel("y", rotation="horizontal", labelpad=10,fontsize=11) ax1.legend(loc=(0.7, 0.7), labelcolor="linecolor", frameon=False) for i,j in enumerate([0.5, 1, 2]): y=[f.subs({a:k, mu:0, s:j}) for k in x] ax2.plot(x, y, color=col[i], label=f"N(0, {j})") ax2.set_xlabel("x\n(b)", fontsize=11) ax2.legend(loc=(0.6, 0.7), labelcolor="linecolor", frameon=False) plt.show()
#그림 7.3.7 x=np.linspace(-3,3, 100) f=np.exp(-x**2/2)/np.sqrt(2*np.pi) plt.figure(figsize=(4,3)) plt.plot(x, f, color="g", label="N(0, 1)") x1=np.linspace(-3,0, 100) f1=np.exp(-x1**2/2)/np.sqrt(2*np.pi) plt.fill_between(x1, f1, color="g", alpha=0.3, label="area=0.5") plt.xlabel("x", fontsize=11) plt.ylabel("f(x)", rotation="horizontal", labelpad=10, fontsize=11) plt.legend(loc="best", labelcolor="g", frameon=False) plt.show()
#그림 7.3.8 a=symbols("a") f=exp(-a**2/2)/sqrt(2*pi) x=np.linspace(-3,3, 100) fig,(ax1, ax2)=plt.subplots(nrows=1, ncols=2, figsize=(8,3)) y=[float(f.subs(a, i)) for i in x] ax1.plot(x, y, color="g", label="N(0, 1)") x1=np.linspace(-3, -1.75, 30) y1=[float(f.subs(a, i)) for i in x1] ax1.fill_between(x1, y1, color="g", alpha=0.3) ax1.legend(loc="best", labelcolor="g", frameon=False) ax1.set_xlabel("x", fontsize=11) ax1.set_xticks([-3, -1.75, 3], ["-oo", -1.75, "oo"]) ax1.set_ylabel("f(x)", rotation="horizontal",labelpad=10, fontsize=11) ax2.plot(x, y, color="g", label="N(0, 1)") x2=np.linspace(-3,2.25, 60) y2=[float(f.subs(a, i)) for i in x2] ax2.fill_between(x2, y2, color="g", alpha=0.3) ax2.set_xlabel("x", fontsize=11) ax2.set_xticks([-3, 2.25, 3], ["-oo", 2.25, "oo"]) ax2.legend(loc="best", labelcolor="g", frameon=False) plt.show()
댓글
댓글 쓰기