본문 바로가기
공부하며놀자/프로그래밍

[python][matplotlib] 파이썬에서 그래프 그리기 plot graph

by 테너토너 2022. 10. 17.
반응형
#reference link
##https://stackoverflow.com/questions/62783909/matplotlib-set-data-not-showing-a-line

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.gridspec as gridspec



#initial grid size with 3rows and 1 column
gs = gridspec.GridSpec(3, 1)

plt.close('all')
plt.ion()

fig = plt.figure(figsize=(10, 12))
fig.suptitle(f'Overall plot name', y=0.94)

graph_row_1 = fig.add_subplot(gs[0, 0])
graph_row_2 = fig.add_subplot(gs[1, 0])
graph_row_3 = fig.add_subplot(gs[2, 0])

graph_row_1.set_title("graph at row 1")
graph_row_2.set_title("graph at row 2")
graph_row_3.set_title("graph at row 3")

plt.show()

name = "testing"


# replotting with different grid size is possible.
# close previous and create new figure and grids
plt.close('all')
plt.ion()
gs = gridspec.GridSpec(2, 1)

fig = plt.figure(figsize=(10, 12))
fig.suptitle(f'Overall plot name', y=0.94)
graph_row_1 = fig.add_subplot(gs[0, 0])
graph_row_2 = fig.add_subplot(gs[1, 0])

plt.show()

name = "sencond"

python으로 graph를 뿌려줘야 하는 일이 생겼다.

공부하면서 중간 중간 기록을 위해 남긴다.

 

간단하게 

figure = 그래프를 그릴 수 있는 스케치북 같은걸로 보면 될 것 같다.

subplot = figure 안에 들어가는 그래프, figure 안에 여러개의 subplot을 만들수가 있다. grid를 만들어서 원하는 cell에 그릴 수 있다.

 

 

예제.

figure에 3개 row가 있는 grid를 만들고, 3개의 subplot을 만든다.

name에 breakpoint 잡아보면 하나의 figure에 3개의 graph가 있는 것을 확인할 수 있다.

확인하고 기존에 것 다 지우고, grid를 row 2개짜리로 새로 변경하고 subplot 두개를 그리도록 되어 있다.

역시나 name에 breakpoint 잡아서 새롭게 2개짜리 subplot으로 바뀐것을 확인할 수 있다.

아직 실제 데이터를 넣지 않아서 원하는 선을 확인할 순 없다. 

선을 그리기 위한 배경화면을 만들었다고 생각하자.

 

 

기본 figure, axes 개념 잡기 좋은 글을 찾았다. 위 예제만 봐도 어느정도 감은 잡히겠지만 읽어두면 좋을 듯

https://towardsdatascience.com/clearing-the-confusion-once-and-for-all-fig-ax-plt-subplots-b122bb7783ca

 

 

반응형

댓글