[ Python ] matplotlib으로 표 그리기.

2018. 8. 17. 11:19언어별 정리 자료/Python

[ Python ] matplotlib으로 표 그리기.



matplotlib 은 데이터 시각화를 담당해주는 시각화 패키지입니다. 내부에 pylab 은 matlab에서 사용하는 데이터 시각화 API를 wrap하여 사용 가능하게 해 줍니다. 직선 / 막대 / 점 그래프 등 다양한 그래프를 숫자, 날짜 등 다양한 기준에 맞춰서 작성 할 수 있습니다. 

코드 예시를 보면서 진행 해 보도롭 합시다.



import matplotlib.pyplot as plt
import sys
import datetime

# 간단한 파일 읽기.
with open("file", "r") as f:
    destLine = f.readlines()    # 파일의 각 줄은 "At 13:16 Laregst Bandwidth : 815904 Bytes" 와 같이 되어있다.

timeList = []
bandwidthList = []
day = 1

for d in destLine:
    xL = datetime.datetime.strptime(d.split(" ")[1], "%H:%M")
    if xL in timeList:
        break
    timeList.append(xL)
    bandwidthList.append(int(d.split(" ")[-2]))

plt.scatter(timeList, bandwidthList)
plt.grid()
plt.xlabel('time')
plt.ylabel('largest bandwidth')
plt.title('Bandwidth Graph')
plt.show()

  • plt.sactter 는 점 그래프를 표현할 때 사용합니다.
    • 반대로 선 그래프는 plot() method를
  • plt.gird() 는 그래프에 격자선을 그려서 보여줍니다.
  • plt.xlabel / ylabel 은 각각 x / y 축에 이름을 붙여줍니다.
  • plt.title은 그래프에 제목을 부여합니다.

사실 사용법 자체는 그렇게 어렵지 않습니다. ( https://datascienceschool.net/view-notebook/d0b1637803754bb083b5722c9f2209d0/ ) 에 정말 잘 설명 되어 있으므로, 소개만 해 주는 선에서 그치려고 합니다.




# 참조


day :     xL = datetime.datetime.strptime(str(day) + " " + d.split(" ")[1], "%d %H:%M") 같이 쓰면 된다.