Python/개념정리
파이썬 막대그래프 [실습]
Itchild
2024. 5. 14. 16:30
728x90
반응형
# 문제
# a.csv에 기온 데이터가 저장되어있습니다.
# 최저 기온의 분포도와 최고 기온의 분포도를 출력해주세요!~~
import csv
import matplotlib.pyplot as plt
maxTempList=[] # 8월 데이터
minTempList=[] # 1월 데이터
with open('a.csv','r') as file:
data=csv.reader(file)
header=next(data)
# 최고 기온 [-1] 최저 기온 [-2]
for row in data:
if row[-1]=='' or row[-2]=='':
continue
month=row[0].split('-')[1]
if month=='08':
maxTempList.append(float(row[-1]))
elif month=='01':
minTempList.append(float(row[-2]))
print(header)
plt.hist(maxTempList,color='red',label='08')
plt.hist(minTempList,color='blue',label='01')
plt.legend()
plt.show()

헤더를 나타내면 이렇게 출력된다 !
최고 기온과 최저 기온을 출력 할 것이므로 [-1] , [-2]
제일 더운날은 8월 일것이라고 예상하고 찾아보자
제일 추운날은 1월 일것이라고 예상하고 찾아보자
최고 기온은 red , 최저 기온은 blue
최고 기온 , 최저 기온은 '' 문자열로 출력 되므로 float 실수 형으로 형변환 !

728x90
반응형