Go together

If you want to go fast, go alone. If you want to go far, go together.

matplotlib

matplotlib 개념

NowChan 2021. 11. 22. 22:47

배운 내용

  1. plt.figure()
  2. Figure.add_subplot()
  3. Axes.set()
  4. Axes.plot()
  5. Axes.scatter()

Figure 생성

plt.figure()

import numpy as np
import matplotlib.pyplot as plt

# 새로운 figure 생성
fig = plt.figure()

# 생성된 모든 figure을 보여준다.
plt.show()
'''
<Figure size 648x648 with 0 Axes>
'''

 

 

plt.figure(figsize=(9, 9))로 figsize에 튜플값을 주면,  창 크기를 가로 세로 9x9 인치로 줄 수 있다. figsize=(6, 9)와 (9, 9)의 차이는 아래와 같다. 실제로 axes를 생성하지 않으면 그래프가 보이지 않지만, 비교를 위해 아래 처럼 그림을 첨부했다.

figsize=(6, 9)   vs   figsize=(9, 9)

 

axes 생성

axes는 figure에 그려지는 좌표 평면과 같다. 실제 그래프가 그려지는 곳은 axes이다.

 

Figure.add_subplot()

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
plt.show()
'''
결과1
'''

결과1

Firgure.subplot(1, 1, 1)은 Figure을 가로 1칸 세로 1칸으로 쪼개고, 1번째 칸에 ax라는 axes를 생성한다는 의미이다.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
plt.show()
'''
결과2
'''

결과2

가로 1칸, 세로 2칸으로 나눈 Figure에 1번째 칸에는 ax1을 그리고, 2번째 칸에는 ax2를 그린다는 의미이다.

 

axes 설정

Axes.set()

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
ax1.set(xlim=[0., 1.], ylim=[-0.5, 2.5], title='Example', xlabel='X', ylabel='y')
plt.show()
'''
결과3
'''

결과3

ax1.set_xlim(0, 1)
ax1.set_ylim(-0.5, 2.5)
ax1.set_title('Example')
ax1.set_xlabel('X')
ax1.set_ylabel('y')

이렇게 axes를 설정할 수도 있다.

 

ax1.set_title('Example', size=20)

글자 크기도 size 파라미터로 설정할 수 있다.

axes 설정 중 size 파라미터 설정

 

 

그래프 그리기

Axes.plot()

Axes.plot()은 꺾은 선 그래프를 그려준다.

X = np.array([1, 3, 6, 4])
y = np.array([10, 20, 25, 45 ])
ax1.plot(X, y, color='red', linewidth=3)
'''
결과4
'''

결과4

 

 

Axes.scatter()

Axes.scatter()은 분산형 그래프를 그려준다.

ax1.scatter(X, y, marker='^', color='darkgreen')
'''
결과5
'''

결과5

 

 

 

 

출처: https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=jung2381187&logNo=220408468960 

 

[Python] matplotlib 01 기초

matplotlib 공식 사이트 : matplotlib.org Python 라이브러리 중 하나로 그림이나 도형을 그려준다. 데이...

blog.naver.com