상세 컨텐츠

본문 제목

데이터분석(3)_Matplotlib

카테고리 없음

by teminam 2023. 5. 23. 10:08

본문

1. Matplotlib

  • 파이썬 기반 시각화 라이브러리
  • 한글에 대한 지원이 완벽하지 않음
  • pandas와 연동이 용이함
  • matplotlib공식 홈페이지[https://matplotlib.org/]
 

!pip install matplotlib
 
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (3.7.1)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.0.7)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (4.39.3)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.4.4)
Requirement already satisfied: numpy>=1.20 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.22.4)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (23.1)
Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (8.4.0)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (3.0.9)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (2.8.2)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
 

#  MATLAB과 비슷하게 명령어 스타일로 동작하는 함수의 모음(pyplot) 
import matplotlib.pyplot as plt
 
 

plt.plot([1, 2, 3, 4])  # plot은 그래프를 그려주는 기능
# 하나의 리스트 값은 y 값을 의미하며, x 값 [0, 1, 2, 3]을 자동으로 생성 
plt.show()  # colab에서는 기봊적으로 show 안해도 보여줌 
 
 
 

plt.plot([1, 2, 3, 4], [1, 4, 10, 15])  # 두개의 리스트를 넣었을 땐 x값, y값/ 하나를 넣었을 땐 y값 하나만 지정 
plt.show()   
 

import numpy as np
data = np.arange(1, 100)
plt.plot(data)
plt.show()
 

data1 = np.arange(1, 50)
plt.plot(data1)
data2 = np.arange(50, 100)
plt.plot(data2)
plt.show()
 

# 여러개의 plot을 그리는 방법
# subplot(row, column, no)
data1 = np.arange(1, 50)
plt.subplot(2, 1, 1)  # 2행 1열, 첫번째 플랏이 1번이다. 
plt.plot(data1)

data2 = np.arange(50, 100)
plt.subplot(2, 1, 2)
plt.plot(data2)

plt.show()
 

# 1행 3열 subplot
data1 = np.arange(0, 100)
plt.subplot(131) # 1행 3열 1번
plt.plot(data1)

data2 = np.arange(0, 100)
plt.subplot(132) # 1행 3열 2번
plt.plot(data2)

data3 = np.arange(0, 100)
plt.subplot(133) # 1행 3열 3번
plt.plot(data3)

plt.show()  
 
 

2. 스타일 옵션

# 코랩의 나눔체를 설치 -> 상단 메뉴 '런타임' -> '다시 시작 및 모두 실행'
!sudo apt-get install -y fonts-nanum
!sudo fc-cache -fv
!rm ~/.cache/matplotlib -rf
 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
fonts-nanum is already the newest version (20180306-3).
0 upgraded, 0 newly installed, 0 to remove and 24 not upgraded.
/usr/share/fonts: caching, new cache contents: 0 fonts, 1 dirs
/usr/share/fonts/truetype: caching, new cache contents: 0 fonts, 3 dirs
/usr/share/fonts/truetype/humor-sans: caching, new cache contents: 1 fonts, 0 dirs
/usr/share/fonts/truetype/liberation: caching, new cache contents: 16 fonts, 0 dirs
/usr/share/fonts/truetype/nanum: caching, new cache contents: 10 fonts, 0 dirs
/usr/local/share/fonts: caching, new cache contents: 0 fonts, 0 dirs
/root/.local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/usr/share/fonts/truetype: skipping, looped directory detected
/usr/share/fonts/truetype/humor-sans: skipping, looped directory detected
/usr/share/fonts/truetype/liberation: skipping, looped directory detected
/usr/share/fonts/truetype/nanum: skipping, looped directory detected
/var/cache/fontconfig: cleaning cache directory
/root/.cache/fontconfig: not cleaning non-existent cache directory
/root/.fontconfig: not cleaning non-existent cache directory
fc-cache: succeeded

plt.rc('font', family='NanumBarunGothic')
 
plt.figure(figsize=(6, 8)) # inch
plt.plot([1, 2, 3], [1, 2, 3])
plt.plot([1, 2, 3], [2, 4, 6])

plt.title('타이틀', fontsize=30)  # 한글은 깨짐  # 위로 올라가서 나눔체 설치하기 
plt.xlabel('X축', fontsize=20)
# plt.ylabel('y축', fontsize=20)
plt.ylabel('Y축', fontsize=20, rotation=0)

plt.show()
 

plt.figure(figsize=(15, 10))

plt.title('마커설정', fontsize=30)
plt.plot(np.arange(10), np.arange(10), color='b', marker='o', linestyle='')
plt.plot(np.arange(10), np.arange(10)*2, color='r', marker='v', linestyle='--')
plt.plot(np.arange(10), np.arange(10)*3, color='y', marker='*', linestyle='-.')

# plt.legend(['10', '10*2', '10*3'], fontsize=15) # 범례/ 기본위치는 왼쪽상단 행3개 
plt.legend(['10', '10*2', '10*3'], fontsize=15, loc='lower right', ncol=3) # 오른쪽 하단의 열로 3개

# x축의 범례길이 설정 
plt.xlim(0, 12) 
plt.ylim(0, 30)

# 눈금의 이름을 틱스라 함
plt.xticks(rotation=30)
plt.yticks(rotation=30)

# 그리드 설정
plt.grid()

plt.show()
 
 

# 막대 그래프 그리기
x = ['파이썬', '데이터분석', '머신러닝', '딥러닝', '컴퓨터비전', '자연어처리']
y = [95 , 80, 65, 30, 20, 10]

plt.figure(figsize=(8, 5))
plt.bar(x, y, align='edge', alpha=0.7, color='blue')
plt.title('AI성적표', fontsize=25)
plt.ylabel('학생점수')
plt.show
 
 

# 막대 그래프 그리기
x = ['파이썬', '데이터분석', '머신러닝', '딥러닝', '컴퓨터비전', '자연어처리']
y = [95 , 80, 65, 30, 20, 10]

plt.figure(figsize=(8, 5))
plt.barh(x, y, align='center', alpha=0.7, color='#e35f62')  # barh:bar horizontal 
plt.title('AI성적표', fontsize=25)
plt.ylabel('학생점수')
plt.show