article thumbnail image
Published 2021. 10. 5. 20:51
728x90
반응형

Line plot

fig, ax = plt.subplots() # .subplots() 인 경우 1개 fig만 생성
x = np.arange(15) # 0~14
y = x ** 2 # x²
ax.plot(
  x, y,
  linestyle=":", # 선을 점으로 표시
  marker="*", # 선 위에 x값을 *모양으로 표시
  color="#524FA1" 
)

Line style 알아보기

x = np.arange(10) # 0~9
fig, ax = plt.subplots()
ax.plot(x, x, linestyle="-")
# solid
ax.plot(x, x+2, linestyle="--")
# dashed
ax.plot(x, x+4, linestyle="-.")
# dashdot
ax.plot(x, x+6, linestyle=":")
# dotted

Line Color

x = np.arange(10)
fig, ax = plt.subplots()
ax.plot(x, x, color="r") # red
ax.plot(x, x+2, color="green")
ax.plot(x, x+4, color="0.8") # grayscale
ax.plot(x, x+6, color="#524FA1")

Marker

x값에 해당하는 값에 marker로 표시

x = np.arange(10)
fig, ax = plt.subplots()
ax.plot(x, x, marker=".")
ax.plot(x, x+2, marker="o")
ax.plot(x, x+4, marker="v")
ax.plot(x, x+6, marker="s")
ax.plot(x, x+8, marker="*")


축 경계 조정하기(linespace)

x = np.linspace(0, 10, 1000) # (start, end, step)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x))
ax.set_xlim(-2, 12) # x축 limit => x축을-2 ~ 12까지로 설정
ax.set_ylim(-1.5, 1.5) # y축을 -1.5 ~ 1.5로 설정


범례(legend)

선이 무슨 색인지 설명

x = np.arange(10)
fig, ax = plt.subplots()
ax.plot(x, x, label='y=x')
ax.plot(x, x**2, label='y=x^2')
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.legend(loc='upper right', # 위쪽 오른쪽
          shadow=True, # 그림자 O
          fancybox=True, # 모서리 둥글게
          borderpad=2) # 보더 크기


예제 👇

from elice_utils import EliceUtils
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

elice_utils = EliceUtils()

x = np.arange(10)
fig, ax = plt.subplots()
ax.plot(
    x, x, label='y=x',
    linestyle='-',
    marker='.',
    color='blue'
)
ax.plot(
    x, x**2, label='y=x^2',
    linestyle='-.',
    marker=',',
    color='red'
)
ax.set_xlabel("x")
ax.set_ylabel("y")

#이미 입력되어 있는 코드의 다양한 속성값들을 변경해 봅시다.
ax.legend(
    loc='center left',
    shadow=True,
    fancybox=True,
    borderpad=2
)

# elice에서 그래프를 확인 => 내가 공부한 사이트
fig.savefig("plot.png")
elice_utils.send_image("plot.png")

반응형

'프로그래밍 > Python' 카테고리의 다른 글

Matplotlib with pandas  (0) 2021.10.07
Matplotlib Bar&Histogram  (0) 2021.10.06
Pandas groupby (2)  (0) 2021.10.01
Pandas groupby (1)  (0) 2021.09.30
Pandas 집계함수  (0) 2021.09.29
복사했습니다!