다음 그림들은 전자책 파이썬과 함께하는 미분적분의 6.1장에 수록된 그래프들과 코드들입니다.
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)
#그림 6.1.1 x=np.linspace(0, 3, 100) y=3*x plt.figure(figsize=(4,3)) plt.plot(x, y, color="g", lw=3, label="f(x)=3x") plt.fill_between(x, y, alpha=0.6) plt.xlabel("Time(hr)", fontsize="11") plt.ylabel("Vel\n(km/hr)", rotation="horizontal", fontsize="11", labelpad=20) plt.legend(loc=(0.2, 0.9), labelcolor='g', frameon=False) plt.show()
#그림 6.1.2 x=[0, 50, 125, 200] y=[400, 400, 300, 250] fig, ax=plt.subplots(figsize=(4,3)) ax.step(x, y, 'g') ax.fill_between(np.linspace(0, 50, 50), np.repeat(400, 50), color="b", alpha=0.3) ax.fill_between(np.linspace(50,125, 50), np.repeat(300, 50), color="r", alpha=0.3) ax.fill_between(np.linspace(125, 200), np.repeat(250, 50), color="orange", alpha=0.3) axisTran(ax) ax.set_xlabel("# of product", fontsize=11) ax.set_ylabel("cost", rotation="horizontal", labelpad=20, fontsize=11) ax.text(0, 350, "C(x)=400", color="b") ax.text(55, 250, "C(x)=300", color="r") ax.text(130, 200, "C(x)=400", color="brown") plt.show()
#그림 6.1.3 a=symbols("a") f=(a-1)*(a-2)*(a-4)+3 x=np.linspace(0, 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="(x-1)(x-2)(x-4)+3") x0=np.linspace(1, 4, 10) for i in range(1, len(x0)): xrng=np.linspace(x0[i-1], x0[i], 20) y0=np.repeat(float(f.subs(a, x0[i-1])), 20) ax.fill_between(xrng, y0, color="b", alpha=0.3) axisTran(ax) ax.set_xlabel("x", fontsize=11) ax.set_ylabel("f(x)", rotation="horizontal", labelpad=10, fontsize=11) ax.set_ylim((-1, 4)) ax.legend(loc=(0.8, 0.8), labelcolor="g", frameon=False) plt.show()
#그림 6.1.4 a=symbols("a") f=600*a-a**2 x=np.linspace(0, 600, 100) y=[f.subs(a, i ) for i in x] fig, ax=plt.subplots(figsize=(4,3)) ax.plot(x, y ,color="g", label=r"$f(x)=600x-x^2$") x0=np.linspace(0, 600, 7) for i in range(1, len(x0)): xrng=np.linspace(x0[i-1], x0[i], 20) y0=np.repeat(float(f.subs(a, x0[i])), 20) ax.fill_between(xrng, y0, color="brown", alpha=0.3) axisTran(ax) ax.set_xlabel("x", fontsize=11) ax.set_ylabel("f(x)", rotation="horizontal", labelpad=10, fontsize=11) ax.legend(loc=(0.8, 0.8), labelcolor="g", frameon=False) plt.show()
#그림 6.1.5 a=symbols("a") f=600*a-a**2 x=np.linspace(0, 600, 100) y=[f.subs(a, i ) for i in x] fig, ax=plt.subplots(figsize=(4,3)) ax.plot(x, y ,color="g", label=r"$f(x)=600x-x^2$") x0=np.linspace(0, 600, 61) for i in range(1, len(x0)): xrng=np.linspace(x0[i-1], x0[i], 20) y0=np.repeat(float(f.subs(a, x0[i])), 20) ax.fill_between(xrng, y0, color="brown", alpha=0.3) axisTran(ax) ax.set_xlabel("x", fontsize=11) ax.set_ylabel("f(x)", rotation="horizontal", labelpad=10, fontsize=11) ax.legend(loc=(0.8, 0.8), labelcolor="g", frameon=False) plt.show()
댓글
댓글 쓰기