파이썬
(MAC OS) matplotlib를 이용한 그래프 그리기
코린이도이
2021. 6. 13. 15:57
step1. 맥북 한글 폰트 설정: AppleGothic이용하기
from matplotlib import font_manager, rc
rc('font', family = 'AppleGothic')
step2. csv파일 안깨지게 불러오기
- 먼저 엑셀 csv 파일을 저장할 때 UTF-8 CSV파일로 저장함
- 한글이 깨지면 engine = 'python'이용
df = pd.read_csv('경로/파일이름.csv', engine = 'python)
⇨ 데이터 프레임 출력 결과
선수명 | 경기수 | 타수 | 득점 | 안타 |
홍길동 | 137 | 476 | 84 | 176 |
일지매 | 131 | 483 | 91 | 177 |
전우치 | 106 | 388 | 84 | 141 |
강감찬 | 125 | 498 | 103 | 173 |
step3. 컬럼별로 데이터 만들기
data1 = df['경기수']
data2 = df['득점']
data3 = df['안타']
name = df['선수명']
step4. 라인그래프 설정
plt.style.use('ggplot')
fig = plt.figure()
ax = fig.add_subplot(111)
step5. 그래프 그리기
plt.style.use('ggplot')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(name, data1, label = '경기수')
ax.plot(name, data2, label = '득점')
ax.plot(name, data3, label = '안타')
step6. 축 제목과 범례 그리기
plt.style.use('ggplot')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(name , data1 , label = '경기수')
ax.plot(name , data2 , label = '득점')
ax.plot(name , data3 , label = '안타')
ax.set_title('선수별 기록')
ax.set_ylabel('건수(단위:건)')
ax.set_xlabel('선수명')
ax.legend(loc=1)
plt.show()