HappyWeasel
파이썬 - 그래프 다루기 본문
matplotlib
- Mathematical Plot Libary
- 파이썬에서 그래프를 그릴 수 있게 하는 라이브러리
- 꺾은선 그래프, 막대 그래프 등을 모두 지원
# matplotlib의 일부인 pyplot 라이브러리를 불러옵니다.
import matplotlib.pyplot as plt
# 엘리스에서 차트를 그릴 때 필요한 라이브러리를 불러옵니다.
from elice_utils import EliceUtils
elice_utils = EliceUtils()
# 월별 평균 기온을 선언합니다. 수정하지 마세요.
years = [2013, 2014, 2015, 2016, 2017]
temperatures = [5, 10, 15, 20, 17]
#막대 차트를 출력합니다.
def draw_graph():
# 막대 그래프의 막대 위치를 결정하는 pos를 선언합니다.
pos = range(len(years)) # [0, 1, 2, 3, 4]
# 높이가 온도인 막대 그래프를 그립니다.
# 각 막대를 가운데 정렬합니다.
plt.bar(pos, temperatures, align='center')
# 각 막대에 해당되는 연도를 표기합니다.
plt.xticks(pos, years)
# 그래프를 엘리스 플랫폼 상에 표시합니다.
plt.savefig('graph.png')
elice_utils.send_image('graph.png')
print('막대 차트를 출력합니다.')
draw_graph()
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
from elice_utils import EliceUtils
elice_utils = EliceUtils()
# 날짜 별 온도 데이터를 세팅합니다.
dates = ["1월 {}일".format(day) for day in range(1, 32)]
temperatures = list(range(1, 32))
# 막대 그래프의 막대 위치를 결정하는 pos를 선언합니다.
pos = range(len(dates))
# 한국어를 보기 좋게 표시할 수 있도록 폰트를 설정합니다.
font = fm.FontProperties(fname='./NanumBarunGothic.ttf')
# 막대의 높이가 빈도의 값이 되도록 설정합니다.
plt.bar(pos, temperatures, align='center')
# 각 막대에 해당되는 단어를 입력합니다.
plt.xticks(pos, dates, rotation='vertical', fontproperties=font)
# 그래프의 제목을 설정합니다.
plt.title('1월 중 기온 변화', fontproperties=font)
# Y축에 설명을 추가합니다.
plt.ylabel('온도', fontproperties=font)
# 단어가 잘리지 않도록 여백을 조정합니다.
plt.tight_layout()
# 그래프를 표시합니다.
plt.savefig('graph.png')
elice_utils.send_image('graph.png')
'Basic > Python' 카테고리의 다른 글
파이썬 - set (0) | 2020.06.04 |
---|---|
파이썬 - json (0) | 2020.06.04 |
파이썬 - 데이터 정렬하기 (0) | 2020.06.03 |
파이썬 - 코드 간결하게 (0) | 2020.06.03 |
파이썬 - 파일 읽고 쓰기 (0) | 2020.06.03 |
Comments