본문 바로가기
마케팅/데이터 분석

[파이썬] Matplotlib

by 퍼포마첼라 2025. 2. 25.

 

Matplotlib과 데이터 시각화

파이썬과 넘파이를 기반으로 데이터를 시각화하는 라이브러리

 

다양한 그래프 그려보기

 

실습

import numpy as np
import matplotlib.pyplot as plt

sales_array = np.array([10, 13, 8, 15, 6, 11, 4])
category_array = ['skirt', 't-shirt', 'dress', 'sweater', 'coat', 'jeans', 'shoes']

# 여기에 코드를 작성하세요.
plt.bar(category_array, sales_array)
plt.show()

결과

 

그래프 간단하게 꾸미기

 

그외 옵션

 

matplotlib.markers — Matplotlib 3.10.0 documentation

matplotlib.markers Functions to handle markers; used by the marker functionality of plot, scatter, and errorbar. All possible markers are defined here: Note that special symbols can be defined via the STIX math font, e.g. "$\u266B$". For an overview over t

matplotlib.org

 

그래프 크기를 설정하는 법

figure()함수 호출하여 figsize 라는 파라미터에 가로, 세로 길이 값을 넘겨준다.(단위는 인치)

일회성이며 다음 그래프를 그릴 때 다시 6*4로 초기화된다. 

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'] = (5, 5)
plt.scatter(height_array, weight_array)
plt.title('Scatter Plot')
plt.xlabel('X Label')
plt.ylabel('Y Label')
plt.show()

 

실습

import numpy as np
import matplotlib.pyplot as plt

age_array = np.array([
	27, 25, 29, 22, 26, 28, 40, 42, 34, 31,
	37, 36, 40, 32, 41, 47, 52, 49, 51, 50,
	49, 46, 47, 46, 47, 45, 57, 58, 60, 56
])

salary_array = np.array([
	44009, 83450, 95025, 63748, 78652,
	58977, 114733, 126353, 121904, 125083,
	143739, 140754, 144346, 114899, 125806,
	180252, 153374, 171057, 161305, 193786,
	167238, 150157, 179843, 195326, 165849,
	164887, 131372, 135876, 113796, 143050
])

# 여기에 코드를 작성하세요.
plt.scatter(age_array, salary_array, c='red', marker='s')
plt.title('Age and Salary')
plt.xlabel('Age')
plt.ylabel('Salary($)')
plt.show()

 

그래프에 한글로된 텍스트 넣기

기본 폰트가 영어 폰트라서 에러가 발생하고 깨진다.

한글 폰트를 넣어주면 사용 가능

 


코드잇 9.Matplotlib

'마케팅 > 데이터 분석' 카테고리의 다른 글

[파이썬] 통계 기본 상식과 그래프  (1) 2025.03.09
[파이썬] pandas  (0) 2025.02.25
[파이썬] NumPy  (0) 2025.02.25
[파이썬] 리스트/사전과 for 반복문  (0) 2025.02.24
[파이썬] while 반복문과 조건문  (0) 2025.02.19