2. Matplotlib

2023. 11. 23. 10:54NAVER AI Tech/Data Visualization

시각화는 plt 라이브러리 내의 figure라는 도화지에 그리게 된다.

여러 그림을 그릴 수 있는데 ax 라는 subplot을 생성하여 지정하게 된다.

import matplotlib.pyplot as plt

fig = plt.figure()

ax1 = fig.add_subplot(1, 2, 1) # 전체 도화지가 1행 2열, 그 중 1번째에 그림을 그려라.
ax2 = fig.add_subplot(1, 2, 2)

plt.show

 

 

데이터 추가하기

fig = plt.figure()

x1 = [1, 2, 3]
x2 = [3, 2, 1]

ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)

ax1.plot(x1)
ax2.plot(x2)

plt.show()

 

같은 그래프에 여러 값 추가하기

fig = plt.figure()

ax = fig.add_subplot(111) # 선그래프와 막대그래프 동시에 그리기
ax.plot([1, 2, 3], [1, 2, 3]) # [x축 위치], [y값]
ax.bar([1, 2, 3], [1, 2, 3]) # [x축 위치], [heights]

plt.show()

 

범례 지정하기

fig = plt.figure()

ax = fig.add_subplot(111)
ax.plot([1, 1, 1], label='1')
ax.plot([2, 2, 2], label='2')
ax.plot([3, 3, 3], label='3')

ax.legend() # legend 가 범례라는 뜻
plt.show()

 

그래프 제목 지정, x축 이름 지정하기, 그래프 위에 텍스트 적기

fig = plt.figure()

ax = fig.add_subplot(111)
ax.plot([1, 1, 1], label='1')
ax.plot([2, 2, 2], label='2')
ax.plot([3, 3, 3], label='3')
ax.set_title('Basic Plot') # 그래프 제목 지정

ax.set_xticks([0, 1, 2]) # x축 지정
ax.set_xticklabels(['zero', 'one', 'two']) # x축 표기 바꾸기

ax.text(x=1, y=2, s='This is Text') # 그래프에 텍스트 추가하기

ax.legend()
plt.show()

 

 

'NAVER AI Tech > Data Visualization' 카테고리의 다른 글

1. Data Visualization 이란?  (0) 2023.11.23