- 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()
DataFrame의 행과 열 - loc() . *.loc[[0,1], ['bookname':'authors']] . *.loc[0:1, 'bookname':'authors'] : DataFrame에서의 slicing은 마지막 값을 포함한다. (일반적으로는 포함하지 않는다)
▶ 기본 미션
p150의 확인 문제 1번 풀고 인증하기
□ 다음과 같은 데이터 프레임 df가 있을 때 loc 메서드의 결과가 다른 하나는 무엇인가요?
① df.loc[[0, 1, 2], ['col1', 'col2']]
② df.loc[0:2, 'col1':'col2']
③ df.loc[:2, [True, True]]
④ df.loc[::2, 'col1':'col2']
보기를 잘 살펴보면 ①, ②는 그냥 보면(?) 되고 ^^ 답은 ③, ④ 중 하나가 될 것이라는 것을 직감적으로 알 수 있다 !!!
③번을 실제 해보면 다음과 같이 출력이 된다.
응?! 'True' 가 도대체 어떻게 반응한다는 거지!? 해당 Column을 포함할지 안할지를 지정하는 것인가!?
그러면 조금 다른 사례를 확인해보자 !!!
그렇다!!! 가설이 맞았다. 3개의 Columns에 대해서 [참, 거짓, 참]으로 했더니 정말로 그렇게 출력이 되었다.
그러면, 정답은 ④이어야 하는데, 한 번 살펴보자.
'::2'라고 했기 때문에 하나씩 건너뛰라고 했기 때문에 0번행, 2번행만 출력이 되고 있다.
▶ 선택 미션
p. 137 ~ 138 손코딩 실습으로 원하는 도서의 페이지 수를 추출하고 화면 캡쳐하기.
① 온라인 서점의 검색 결과 페이지 URL을 만듭니다.
② requests.get() 함수로 검색 결과 페이지의 HTML을 가져옵니다.
③ BeautifulSoup로 HTML을 파싱합니다.
④ BeautifulSoup의 find() 메서드로 <a> 태그를 찾아 상세 페이지 URL을 추출합니다.
❯ ~/miniconda3/bin/conda init zsh no change /home/chani22/miniconda3/condabin/conda no change /home/chani22/miniconda3/bin/conda no change /home/chani22/miniconda3/bin/conda-env no change /home/chani22/miniconda3/bin/activate no change /home/chani22/miniconda3/bin/deactivate no change /home/chani22/miniconda3/etc/profile.d/conda.sh no change /home/chani22/miniconda3/etc/fish/conf.d/conda.fish no change /home/chani22/miniconda3/shell/condabin/Conda.psm1 no change /home/chani22/miniconda3/shell/condabin/conda-hook.ps1 no change /home/chani22/miniconda3/lib/python3.11/site-packages/xontrib/conda.xsh no change /home/chani22/miniconda3/etc/profile.d/conda.csh modified /home/chani22/.zshrc
==> For changes to take effect, close and re-open your current shell. <==
마지막의 init은 새로운 버전으로 업데이트하거나 했을 때 초기화 하는 것인데 그냥 첫 설치 때도 해봤다.
Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate python_39 # # To deactivate an active environment, use # # $ conda deactivate
❯ jupyter notebook [W 2024-01-11 00:29:46.408 ServerApp] A `_jupyter_server_extension_points` function was not found in jupyter_lsp. Instead, a `_jupyter_server_extension_paths` function was found and will be used for now. This function name will be deprecated in future releases of Jupyter Server. [W 2024-01-11 00:29:46.429 ServerApp] A `_jupyter_server_extension_points` function was not found in notebook_shim. Instead, a `_jupyter_server_extension_paths` function was found and will be used for now. This function name will be deprecated in future releases of Jupyter Server. [I 2024-01-11 00:29:46.429 ServerApp] jupyter_lsp | extension was successfully linked. [I 2024-01-11 00:29:46.432 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2024-01-11 00:29:46.435 ServerApp] jupyterlab | extension was successfully linked. [I 2024-01-11 00:29:46.439 ServerApp] notebook | extension was successfully linked. [I 2024-01-11 00:29:46.440 ServerApp] Writing Jupyter server cookie secret to /home/chani22/.local/share/jupyter/runtime/jupyter_cookie_secret [I 2024-01-11 00:29:46.604 ServerApp] notebook_shim | extension was successfully linked. [I 2024-01-11 00:29:46.618 ServerApp] notebook_shim | extension was successfully loaded. [I 2024-01-11 00:29:46.619 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2024-01-11 00:29:46.620 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2024-01-11 00:29:46.621 LabApp] JupyterLab extension loaded from /home/chani22/miniconda3/envs/python_39/lib/python3.9/site-packages/jupyterlab [I 2024-01-11 00:29:46.621 LabApp] JupyterLab application directory is /home/chani22/miniconda3/envs/python_39/share/jupyter/lab [I 2024-01-11 00:29:46.622 LabApp] Extension Manager is 'pypi'. [I 2024-01-11 00:29:46.624 ServerApp] jupyterlab | extension was successfully loaded. [I 2024-01-11 00:29:46.625 ServerApp] notebook | extension was successfully loaded. [I 2024-01-11 00:29:46.626 ServerApp] Serving notebooks from local directory: /srv/workspace/python_39 [I 2024-01-11 00:29:46.626 ServerApp] Jupyter Server 2.12.3 is running at: [I 2024-01-11 00:29:46.626 ServerApp] http://localhost:8888/tree?token=5ed486a3d676270879b3684991a11b4932c981c265dddef4 [I 2024-01-11 00:29:46.626 ServerApp] http://127.0.0.1:8888/tree?token=5ed486a3d676270879b3684991a11b4932c981c265dddef4 [I 2024-01-11 00:29:46.626 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 2024-01-11 00:29:47.025 ServerApp]
To access the server, open this file in a browser: file:///home/chani22/.local/share/jupyter/runtime/jpserver-4428-open.html Or copy and paste one of these URLs: http://localhost:8888/tree?token=5ed486a3d676270879b3684991a11b4932c981c265dddef4 http://127.0.0.1:8888/tree?token=5ed486a3d676270879b3684991a11b4932c981c265dddef4 [I 2024-01-11 00:29:47.041 ServerApp] Skipped non-installed server(s): bash-language-server, dockerfile-language-server-nodejs, javascript-typescript-langserver, jedi-language-server, julia-language-server, pyright, python-language-server, python-lsp-server, r-languageserver, sql-language-server, texlab, typescript-language-server, unified-language-server, vscode-css-languageserver-bin, vscode-html-languageserver-bin, vscode-json-languageserver-bin, yaml-language-server