- Matplotlib
- CSV : 분석 데이터를 그래프나 차트로 시각화해주는 파이썬 패키지 라이브러리
- Matplotlib에 포함된 pyplot 모듈은 매트랩(MATLAB)처럼 동작하는 명령어 형태의 함수를 제공
실습
import csv
import matplotlib.pyplot as plt
from matplotlib import rc
rc('font', family='Malgun Gothic')
f = open('cafe_2year.csv','r',encoding='utf-8')#csv파일 불러옴
lines = csv.reader(f) #리더를 통해 lines에 초기화
header = next(lines)
xdata = [] #x축 데이터
ydata = [] #y축 데이터
count = 1
for line in lines :
if count%3 == 0: #3개월 간격으로
xdata.append(line[0]) #x축에 연도 저장
ydata.append(line[1]) #y축에 카페수 저장
count += 1
plt.bar(xdata,ydata) #바를 통해서 표현
plt.title('전국의 월별 카페수')
plt.ylabel('카페수')
plt.xlabel('년월')
plt.show()
f.close()
import csv
import matplotlib.pyplot as plt
f = open('doctor_2019.csv','r',encoding='utf-8')
lines = csv.reader(f)
from matplotlib import rc
rc('font', family = 'Malgun Gothic')
header = next(lines)
print(header[0], header[2], header[3], header[4], header[5])
#지역명을 리스트로 초기화
local = ['서울', '부산', '대구' ,'인천','대전','광주','울산']
#의사종류가 4개이고, 지역이 7개
doctor = ([[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0],
[0,0,0,0], [0,0,0,0], [0,0,0,0]])
for line in lines :
if line[0] == '서울': #지역에 서울이 포함되면,
doctor[0][0] += int(line[2])
doctor[0][1] += int(line[3])
doctor[0][2] += int(line[4])
doctor[0][3] += int(line[5])
if line[0] == '부산':
doctor[1][0] += int(line[2])
doctor[1][1] += int(line[3])
doctor[1][2] += int(line[4])
doctor[1][3] += int(line[5])
if line[0] == '대구':
doctor[2][0] += int(line[2])
doctor[2][1] += int(line[3])
doctor[2][2] += int(line[4])
doctor[2][3] += int(line[5])
if line[0] == '인천' :
doctor[3][0] += int(line[2])
doctor[3][1] += int(line[3])
doctor[3][2] += int(line[4])
doctor[3][3] += int(line[5])
if line[0] == '대전' :
doctor[4][0] += int(line[2])
doctor[4][1] += int(line[3])
doctor[4][2] += int(line[4])
doctor[4][3] += int(line[5])
if line[0] == '광주' :
doctor[5][0] += int(line[2])
doctor[5][1] += int(line[3])
doctor[5][2] += int(line[4])
doctor[5][3] += int(line[5])
if line[0] == '울산' :
doctor[6][0] += int(line[2])
doctor[6][1] += int(line[3])
doctor[6][2] += int(line[4])
doctor[6][3] += int(line[5])
#출력
for i in range(7): #지역이 7개이니
print(local[i], end='') #로컬의 i번째에
for j in range(4): #닥터가 4명종류 j번째
print('%8d'%doctor[i][j], end='') #반복해서 출력
print()
f.close()
import csv
file1 = open('doctor_2019.csv','r',encoding='utf-8')
file2 = open('doctor_20192.csv','w',encoding='utf-8', newline='')
lines = csv.reader(file1)
wr = csv.writer(file2)
header = next(lines)
local = ['서울', '부산', '대구', '인천', '대전', '광주', '울산']
doctor = ([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0],
[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
for line in lines:
if line[0] == '서울' :
doctor[0][0] += int(line[2])
doctor[0][1] += int(line[3])
doctor[0][2] += int(line[4])
doctor[0][3] += int(line[5])
if line[0] == '부산' :
doctor[1][0] += int(line[2])
doctor[1][1] += int(line[3])
doctor[1][2] += int(line[4])
doctor[1][3] += int(line[5])
if line[0] == '대구' :
doctor[2][0] += int(line[2])
doctor[2][1] += int(line[3])
doctor[2][2] += int(line[4])
doctor[2][3] += int(line[5])
if line[0] == '인천' :
doctor[3][0] += int(line[2])
doctor[3][1] += int(line[3])
doctor[3][2] += int(line[4])
doctor[3][3] += int(line[5])
if line[0] == '대전' :
doctor[4][0] += int(line[2])
doctor[4][1] += int(line[3])
doctor[4][2] += int(line[4])
doctor[4][3] += int(line[5])
if line[0] == '광주' :
doctor[5][0] += int(line[2])
doctor[5][1]+= int(line[3])
doctor[5][2] += int(line[4])
doctor[5][3] += int(line[5])
if line[0] == '울산' :
doctor[6][0] += int(line[2])
doctor[6][1] += int(line[3])
doctor[6][2] += int(line[4])
doctor[6][3] += int(line[5])
wr = csv.writer(file2)
wr.writerow(['지역', '일반의 수','인턴 수','레지던트 수','전문의 수'])
for i in range(7):
wr.writerow([local[i],doctor[i][0],doctor[i][1],
doctor[i][2],doctor[i][3]])
print('doctor2.csv 파일 쓰기 완료!')
file1.close()
file2.close()
import csv
import matplotlib.pyplot as plt
from matplotlib import rc
f = open('doctor_20192.csv','r',encoding='utf-8')
lines = csv.reader(f)
rc('font', family='Malgun Gothic')
header = next(lines)
xdata = []
ydata = []
for line in lines :
xdata.append(line[0])
ydata.append(int(line[4]))
plt.title('서울 및 광역시 종합병원 전문의 수')
plt.scatter(xdata,ydata)
plt.xlabel('시')
plt.ylabel('의사 수')
plt.show()
f.close()
import csv
import matplotlib.pyplot as plt
from matplotlib import rc
f = open('doctor_20192.csv','r',encoding='utf-8')
lines = csv.reader(f)
rc('font', family = 'Malgun Gothic')
header = next(lines)
xdata = []
ydata1 = []
ydata2 = []
ydata3 = []
ydata4 = []
for line in lines :
xdata.append(line[0])
ydata1.append(int(line[1]))
ydata2.append(int(line[2]))
ydata3.append(int(line[3]))
ydata4.append(int(line[4]))
plt.plot(xdata, ydata1, label = header[1], color = 'red', linestyle ='--', marker='o')
plt.plot(xdata, ydata2, label = header[2], color = 'blue', linestyle ='-', marker='x')
plt.plot(xdata, ydata3, label = header[3], color = 'yellow', linestyle ='-.', marker='s')
plt.plot(xdata, ydata4, label = header[4], color = 'purple', linestyle =':', marker='d')
plt.title('서울 및 광역시 종합병원 의사 수')
plt.legend(loc='best')
plt.show()
f.close()
import csv
import matplotlib.pyplot as plt
from matplotlib import rc
f = open('doctor_20192.csv','r',encoding='utf-8')
lines = csv.reader(f)
rc('font', family = 'Malgun Gothic')
header = next(lines)
xdata = [header[1],header[2],header[3],header[4]] #의사종류
ydata = [] #
seoul = next(lines)
total = 0
for i in range(4):
total += int(seoul[i+1])
for i in range(4):
persent = int(seoul[i+1])/total
ydata.append(persent)
plt.pie(ydata, explode=(0, 0.1, 0, 0), labels=xdata, autopct='%.1f%%', shadow=True, startangle=90)
plt.title('서울 종합병원 의사 유형별 분포')
plt.show()
f.close()
'PYTHON' 카테고리의 다른 글
[파이썬] Numpy (0) | 2022.05.12 |
---|---|
[파이썬] 기초 연습문제 (0) | 2022.05.10 |
[파이썬] 예외처리 (0) | 2022.05.10 |
[파이썬] 객체지향 프로그래밍 (0) | 2022.05.10 |
[파이썬] 함수 (0) | 2022.05.10 |