[Python] Matplotlib 평면내 여러 개의 선 그래프 그리기
오늘은 한 평면 내에 여러 개의 선 그래프를 그리는 것을 공부하겠습니다.
예제로 2018학년도부터 2022학년도 까지
총 5개년 수능 과학탐구 과목별 응시자 수를 그래프로 그려보겠습니다.
라이브러리 불러오기
import matplotlib.pyplot as plt
한글 오류 기본 설정
plt.rc('font', family = 'AppleGothic') # mac
# plt.rc('font', family = 'Malgun Gothic') # window
plt.rc('font', size = 12)
plt.rc('axes', unicode_minus = False) # -표시 오류 잡아줌
전 포스트에 적어놨던 한글 오류 설정입니다.
수능 과탐 데이터 리스트
# 년도
year = [2018, 2019, 2020, 2021, 2022]
# 만명 단위
physics = [6.2,6.3 ,6.0 ,5.9 ,6.8]
chemistry = [10.6,9.4 ,8.0 ,8.0 ,8.0]
life = [16.1,16.3 ,13.9 ,13.1 ,14.7]
earth = [16.7,17.7 ,16.1 ,13.0 ,14.9]
년도는 2018학년도 ~ 2022학년도까지 있습니다.
실제 데이터를 가져와 작성을 했고
physics, chemistry, life, earth 각각
물리, 화학, 생명과학, 지구과학 응시자 수입니다.
단위는 만 명 기준입니다.
여러 개의 선 그래프 그리기
plt.figure(figsize = (10, 5))
plt.plot(year, physics)
plt.plot(year, chemistry)
plt.plot(year, life)
plt.plot(year, earth)
plt.show()

공통인 x축(학년도)을 각각 plot에 넣어주고
각 과목 데이터를 y축에 바꾸면서 넣어주면 됩니다.
제목(title)과 축 레이블 달기
plt.figure(figsize = (10, 5))
plt.title('수능 과학탐구 응시자 수')
plt.plot(year, physics)
plt.plot(year, chemistry)
plt.plot(year, life)
plt.plot(year, earth)
plt.show()

위와 같이 수능 과학탐구 응시자 수라는 제목을 달아줬습니다.
이번에는 x축과 y축에 레이블을 달아주겠습니다.
plt.figure(figsize = (10, 5))
plt.title('수능 과학탐구 응시자 수')
plt.xlabel('학년도')
plt.ylabel('응시자 수(단위 : 만명)')
plt.plot(year, physics)
plt.plot(year, chemistry)
plt.plot(year, life)
plt.plot(year, earth)
plt.show()

x축에 학년도, y축에 응시자 수(단위 : 만 명)이라고 레이블을 달아줬습니다.
마커(marker)와 선 꾸미기
plt.figure(figsize = (10, 5))
plt.title('수능 과학탐구 응시자 수')
plt.xlabel('학년도')
plt.ylabel('응시자 수(단위 : 만명)')
plt.plot(year, physics, marker = 'o')
plt.plot(year, chemistry, marker = 'd')
plt.plot(year, life, marker = 'v')
plt.plot(year, earth, marker = 'x')
plt.show()

위와 같이 각각의 선에 마커를 달아줬는데
마커의 크기가 작아서 크게 키워주겠습니다.
plt.figure(figsize = (10, 5))
plt.title('수능 과학탐구 응시자 수')
plt.xlabel('학년도')
plt.ylabel('응시자 수(단위 : 만명)')
plt.plot(year, physics, marker = 'o', ms = 10)
plt.plot(year, chemistry, marker = 'd', ms = 10)
plt.plot(year, life, marker = 'v', ms = 10)
plt.plot(year, earth, marker = 'x', ms = 10)
plt.show()

마커가 조금 커진 것을 확인할 수 있죠.
이번에는 선 스타일을 바꿔주겠습니다.
plt.figure(figsize = (10, 5))
plt.title('수능 과학탐구 응시자 수')
plt.xlabel('학년도')
plt.ylabel('응시자 수(단위 : 만명)')
plt.plot(year, physics, marker = 'o', ms = 10, ls = '-')
plt.plot(year, chemistry, marker = 'd', ms = 10, ls = '-.')
plt.plot(year, life, marker = 'v', ms = 10, ls = '--')
plt.plot(year, earth, marker = 'x', ms = 10, ls = ':')
plt.show()

각각의 선의 스타일이 다른 것을 확인할 수 있죠.
범례 표시
plt.figure(figsize = (10, 5))
plt.title('수능 과학탐구 응시자 수')
plt.xlabel('학년도')
plt.ylabel('응시자 수(단위 : 만명)')
plt.plot(year, physics, marker = 'o', ms = 10, ls = '-', label = '물리학')
plt.plot(year, chemistry, marker = 'd', ms = 10, ls = '-.', label = '화학')
plt.plot(year, life, marker = 'v', ms = 10, ls = '--', label = '생명과학')
plt.plot(year, earth, marker = 'x', ms = 10, ls = ':', label = '지구과학')
plt.legend()
plt.show()

각각의 plot에 label을 달아주고
legend를 사용하여 범례를 표시해주었습니다.
default가 행을 기준으로 범례를 표시하므로
이번에는 열을 기준으로 표시하면서 여백이 있는
오른쪽 위로 범례를 옮기겠습니다.
plt.figure(figsize = (10, 5))
plt.title('수능 과학탐구 응시자 수')
plt.xlabel('학년도')
plt.ylabel('응시자 수(단위 : 만명)')
plt.plot(year, physics, marker = 'o', ms = 10, ls = '-', label = '물리학')
plt.plot(year, chemistry, marker = 'd', ms = 10, ls = '-.', label = '화학')
plt.plot(year, life, marker = 'v', ms = 10, ls = '--', label = '생명과학')
plt.plot(year, earth, marker = 'x', ms = 10, ls = ':', label = '지구과학')
plt.legend(ncol = 4, loc = (0.3, 0.9))
plt.show()

legend안에 ncol = 4를 넣어주고
loc로 location을 (0.3, 0.9)로 변경해줬습니다.
다음 시간에는 그래프에 텍스트를 추가하는 방법을 알아보겠습니다!