본문 바로가기
수학

제1장 완성

by 자동매매 2023. 12. 22.

기본 설정

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits import mplot3d
import matplotlib.font_manager as mfm

# numpy 출력 형식 지정
np.set_printoptions(precision=4, linewidth=150)


# matplotlib 스타일 지정
st = plt.style.available  # 사용가능 스타일 확인
mpl.style.use("bmh")
mpl.style.use("seaborn-v0_8-whitegrid")
style = plt.style.library["bmh"]

# 스타일 컬러를 쉽게 쓸 수 있도록 리스트 저장
style_colors = [c["color"] for c in style["axes.prop_cycle"]]

# 그림을 컬러로 출력하고 싶으면 True로 수정
g_color = True

# 그림을 로컬 폴더에 저장하고 싶으면 True로 수정
file_print = True

# Unicode warning 제거 (폰트 관련 경고메시지)
plt.rcParams["axes.unicode_minus"] = False

# 한글 폰트 설정
plt.rcParams["font.family"] = "Malgun Gothic"

# 그래프 출력 사이즈 설정
plt.rcParams["figure.figsize"] = (10, 8)

 

 

좌표 설정

def arrowed_spines(fig, ax, remove_ticks=False):
    """
    좌표축 화살표를 그리기 위한 함수
    https://stackoverflow.com/questions/33737736/matplotlib-axis-arrow-tip
    """
    xmin, xmax = ax.get_xlim()
    ymin, ymax = ax.get_ylim()

    # removing the default axis on all sides:
    for side in ["bottom", "right", "top", "left"]:
        ax.spines[side].set_visible(False)

    if remove_ticks == True:
        # removing the axis ticks
        plt.xticks([])  # labels
        plt.yticks([])
        ax.xaxis.set_ticks_position("none")  # tick markers
        ax.yaxis.set_ticks_position("none")

    # get width and height of axes object to compute
    # matching arrowhead length and width
    dps = fig.dpi_scale_trans.inverted()
    bbox = ax.get_window_extent().transformed(dps)
    width, height = bbox.width, bbox.height

    # manual arrowhead width and length
    hw = 1.0 / 50.0 * (ymax - ymin)
    hl = 1.0 / 25.0 * (xmax - xmin)
    lw = 1.0  # axis line width
    ohg = 0.4  # arrow overhang

    # compute matching arrowhead length and width
    yhw = hw / (ymax - ymin) * (xmax - xmin) * height / width
    yhl = hl / (xmax - xmin) * (ymax - ymin) * width / height

    # draw x and y axis
    ax.arrow(
        xmin,
        0,
        xmax - xmin,
        0.0,
        fc="k",
        ec="k",
        lw=lw,
        head_width=hw,
        head_length=hl,  # overhang = ohg,
        length_includes_head=True,
        clip_on=False,
    )

    ax.arrow(
        0,
        ymin,
        0.0,
        ymax - ymin,
        fc="k",
        ec="k",
        lw=lw,
        head_width=yhw,
        head_length=yhl,  # overhang = ohg,
        length_includes_head=True,
        clip_on=False,
    )

 

 

테스트

fig = plt.figure()

ax = fig.add_subplot(1, 1, 1)
ax.xaxis.set_tick_params(labelsize=18)
ax.yaxis.set_tick_params(labelsize=18)
ax.set_xlabel("$x$축", fontsize=25)
ax.set_ylabel("$y$축", fontsize=25)

ax.set_xlim(-6, 6)
ax.set_ylim(-6, 6)

x = np.linspace(-5, 5, 50)
xx, yy = np.meshgrid(x, x)

plt.plot(xx, yy, ".", markersize=1, color='r')

arrowed_spines(fig, ax)

if file_print == True:
    fig.savefig("imgs/chap2/fig2-3.png", dpi=300, bbox_inches="tight")
    fig.savefig("imgs/chap2/fig2-3.pdf", format="pdf", bbox_inches="tight")

plt.show()

 

 

fig = plt.figure()
ax = fig.add_subplot(111)
ax.xaxis.set_tick_params(labelsize=15)
ax.yaxis.set_tick_params(labelsize=15)
ax.set_xlabel("$x$", fontsize=25)
ax.set_ylabel("$y$", fontsize=25)
ax.set_title("제목", fontsize=30)


x = np.linspace(-5, 5, 100)
y = x**2

ax.plot(x, y, color="r")

arrowed_spines(fig, ax)

plt.show()

 

 

'수학' 카테고리의 다른 글

제5장  (0) 2023.12.27
제4장  (0) 2023.12.27
제3장  (0) 2023.12.27
제2장  (0) 2023.12.27
제1장  (1) 2023.12.27

댓글