자신에게 친절할 것 :)

Data Science/Visualization

[matplotlib] graph 제목 붙이기, 사이즈 조절, 한글 제목 넣기

Tashapark 2024. 5. 31. 18:42
728x90

#코드잇 데이터 사이언스 강의 듣는 중

#데이터 사이언스 Toolkit  renewal 버전 다시 듣는 중


 

- scatter plot 산포도 plt.scatter(x, y)

- ... 산포도는 진짜..30개 랜덤이라서.. 걍 만들었는데, 그대로 하니깐 너무 상관이 안 뜨게 나타나서, 사실 몸무게와 키는 다소 비례하기 때문에 sorted를 해줬음. 

- 문제는 그래서 사실상의 linear가 나와버렸지만.. 실제로는 아님. 

 height_array = np.array([157, 175, 190, 162, 150, 164, 178, 188, 189, 161, 159, 186, 175, 163, 158, 165, 164, 176, 173, 160, 174, 184, 186, 152, 176, 158, 179, 160, 167, 186])
sorted_height_array = sorted(height_array)

weight_array = np.array([74, 54, 86, 79, 58, 81, 70, 51, 65, 69, 64, 85, 70, 56, 56, 55, 56, 53, 77, 81, 52, 53, 58, 85, 61, 83, 73, 61, 65, 85])
sorted_weight_array = sorted(weight_array)

plt.scatter(sorted_height_array, sorted_weight_array)
plt.show()

 

- 제목이랑 각 축 설명하는 코드는 매우 간단함. 

- plt.title('') --> 제목

- plt.xlabel('') --> x축

- plt.ylabel('') --> y축 

 

++

- 색을 바꾸는 것은  .scatter(, c='')로 스캐터 메소드에 조건을 추가해주면 됨. 

- 'red', 'green' 도 되고 '#6500C3"과 같은 컬러 헥스 코드 형태도 가능함. 

 

- marker의 모양도 바꿀 수 있는데, .scatter(, marker='')로 스캐터 메소드에 조건을 추가하면 됨. 

plt.scatter(sorted_height_array, sorted_weight_array, c='#6500C3', marker='+')
plt.title('Height and Weight')
plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
plt.show()

 

 

+++ 여러 마커 옵션들은 아래 링크에서 확인이 가능함

https://matplotlib.org/stable/api/markers_api.html#module-matplotlib.markers

 

matplotlib.markers — Matplotlib 3.9.0 documentation

matplotlib.markers Functions to handle markers; used by the marker functionality of plot, scatter, and errorbar. All possible markers are defined here: marker symbol description "." point "," pixel "o" circle "v" triangle_down "^" triangle_up "<" triangle_

matplotlib.org

 

 

- 그래프 사이즈 조절 방법

- figure(figsize = (x, y)) --> figure() 함수에 figsize 파라미터 값을 가로, 세로 순서로 넣어주면 됨. 

- 기준은 인치(inch)임. 

 

import numpy as np
import matplotlib.pyplot as plt

height_array = np.array([
    165, 164, 155, 151, 157, 162, 155, 157, 165, 162,
    165, 167, 167, 183, 180, 184, 177, 178, 175, 181,
    172, 173, 169, 172, 177, 178, 185, 186, 190, 187
])

weight_array = np.array([
    62, 59, 57, 55, 60, 58, 51, 56, 68, 64,
    57, 58, 64, 79, 73, 76, 61, 65, 83, 80,
    67, 82, 88, 62, 61, 79, 81, 68, 83, 80
])

plt.figure(figsize=(10,4))
plt.scatter(height_array, weight_array)
plt.title('Height and Weight')
plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
plt.show()

 

 

- 그래프 사이즈를 매번 바꾸기 싫다면, rcParams 속성을 변경하면 됨. 

- plt.rcParams['figure.figsize'] = (x, y)

plt.rcParams['figure.figsize'] = (5,5)
plt.scatter(height_array, weight_array)
plt.title('Height and Weight')
plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
plt.show()

 

 

 

- 한글로 제목 넣기

- 한글 폰트가 없기 때문에 한글로 제목을 쓰면 읽히지 않음. 

- plt.rc('font', family = '') --> 글씨체를 넣어주면 됨. 

- 강의에서는 'Apple Gothic'으로 띄어쓰기를 하라고 하셨지만,, 이유는 모르겠으나 나는 띄어쓰면 폰트 에러가 뜸. 

- 'AppleGothic' 그래서 합쳐서 했음. 

- 윈도우 유저들은 'MalgunGothic'으로 하면 됨. (맥도 적용 됨)

.. 근데 한글이 별로 안 예쁘게 나오는 듯.

 

plt.scatter(height_array, weight_array, c='#6500C3', marker='s')
plt.rc('font', family='AppleGothic')
plt.title('키와 몸무게')
plt.xlabel('키 (cm)')
plt.ylabel('몸무게 (kg)')
plt.show()

728x90
반응형