- plt.figure(figsize=(x, y)) : 그래프 크기 - plt.figure(dpi=d) : DPI . plt.figure(figsize=(x/d, y/d)) : 이처럼 계산하면 크기를 계산하면서 조절할 수 있다 - plt.rcParams[parameter] : 관련 속성값을 확인하거나 설정할 수 있음 - fig, axs = plt.subplots() : 그래프 묶음 관리. axs 를 통해 각 그래프를 지
5-2 선 그래프와 막대 그래프 그리기
- plt.plot() : 선 그래프
plt.plot(count_by_year.index, count_by_year.values) plt.title('Books by year') plt.xlabel('year') plt.ylabel('number of books') plt.show()
- marker='.', linestyle=':', color='red' : 꾸미기 옵션 . '*-g' : 여러 옵션을 한 방에 해치우기 - plt.xticks() : 눈금값 설정하기
plt.plot(count_by_year, '*-g') plt.title('Books by year') plt.xlabel('year') plt.ylabel('number of books') plt.xticks( range(1947, 2030, 10) ) plt.show()
- plt.annotate() : 값 텍스트 출력 . xytext : 출력 위치 보정 . textcoords : 상대값으로 위치 보정
plt.plot(count_by_year, '*-g') plt.title('Books by year') plt.xlabel('year') plt.ylabel('number of books') plt.xticks( range(1947, 2030, 10) ) for idx, val in count_by_year[::5].items(): # plt.annotate(val, (idx, val), xytext=(idx+1, val+10)) plt.annotate(val, (idx, val), xytext=(2, 2), textcoords='offset points') plt.show()
- plt.bar() : 막대 그래프
plt.bar(count_by_subject.index, count_by_subject.values) plt.title('Books by subject') plt.xlabel('subject') plt.ylabel('number of books') plt.xticks( range(1947, 2030, 10) ) for idx, val in count_by_subject.items(): plt.annotate(val, (idx, val), xytext=(0, 2), textcoords='offset points') plt.show()
- plt.annotate() : 값 텍스트 출력 . fontsize : 폰트 크기 . ha : 정렬
plt.bar(count_by_subject.index, count_by_subject.values, width=0.7, color='blue' ) plt.title('Books by subject') plt.xlabel('subject') plt.ylabel('number of books') plt.xticks( range(1947, 2030, 10) ) for idx, val in count_by_subject.items(): plt.annotate(val, (idx, val), xytext=(0, 2), textcoords='offset points', fontsize=9, va='center', color='green') plt.show()