본문 바로가기

Python_Wiki/Python_Library26

Plotly 기초 / 스타일 설정하기 #주요 시각화 라이브러리#matplotlib: standard, seaborn: advanced version of matplotlib, plotly: new and interactive viz libimport plotly.express as pxfig = px.그래프종류(data_frame=데이터, x=X축 컬럼, y=Y축 컬럼, color=범례 컬럼, title=제목, labels=dict(X축 컬럼=X축 라벨, Y축 컬럼=Y축 라벨), width=그래프 가로길이, height=그래프 세로길이, text_auto=True/False)fig.show()#그래프종류: line, bar, box...#examplefig = px.bar(df_groupby, x='species', y='body_mass_.. 2025. 7. 14.
matplotlib / seaborn 고화질 설정 %config InlineBackend.figure_format = 'retina'#example%config InlineBackend.figure_format = 'retina'sns.barplot(data=df, x='species', y='body_mass_g')plt.title('펭귄의 종별 몸무게')plt.show() 2025. 7. 14.
matplotlib / seaborn 한글 적용하기 1. 한글폰트 설치 준비!sudo apt-get install -y fonts-nanum!sudo fc-cache -fv!rm ~/.cache/matplotlib -rf!sudo apt-get install -y fonts-nanum #sudo: 관리자 권한으로 실행 #-y: "예/아니오" 묻지 말고 자동 진행 #fonts-nanum 패키지를 리눅스 시스템에 설치!sudo fc-cache -fv #폰트 캐시를 초기화 및 갱신하는 명령어!rm ~/.cache/matplotlib -rf #matplotlib의 폰트 캐시를 삭제 2. 런타임 다시 시작3. 라이브러리 재 실행 (matplotlib 등)4. 한글 폰트 적용plt.rc('font', family='NanumGo.. 2025. 7. 14.
Seaborn 스타일 설정 sns.set_style(스타일) #스타일: darkgrid, whitegrid, dark, whitesns.set_palette(팔레트) #팔레트: 기본 세트는 아래 링크 참조#examplesns.set_style('white')sns.set_palette('flare')sns.barplot(data=df, x='species', y='body_mass_g')plt.show()공식문서 스타일 링크 Choosing color palettes — seaborn 0.13.2 documentationChoosing color palettes Seaborn makes it easy to use colors that are well-suited to the characteristics of your data .. 2025. 7. 14.
pandas: stack, unstack, melt (데이터 구조 변경) #stack : 컬럼 레벨에서 인덱스 레벨로 데이터프레임을 변경합니다.#unstack : 인덱스 레벨에서 컬럼 레벨로 데이터프레임을 변경합니다.pivot = pd.pivot_table(df, index=['Sex','Pclass'], values=['Survived','Fare'], aggfunc=['mean','median','sum'])pivot .stack(숫자, future_stack=True) #숫자만 쓰는것은 나중에 없어질 예정이라, future_stack=True를 꼭 같이 써주자.pivot.stack(0, future_stack=True) #컬럼의 첫번째 레벨을 인덱스로 내립니다.#0대신 1쓰면 두번째 레벨을 인덱스로 내림.unstack(숫자)pivot.unstack(1) #인덱스의 두번째.. 2025. 7. 14.
pandas: 피벗테이블 만들기 .pivot_table /+) pivot 피벗테이블은 crosstab이랑은 다르게, 엑셀 피벗테이블처 값 영역에 변수를 별도로 넣어서 계산을 할 수 있음#index 행, columns 열, values 값에 넣을 열, aggfunc 집계함수pd.pivot_table(df, index='Sex', columns='Pclass', values='Survived', aggfunc='mean') #crosstab처럼 margins 가능pd.pivot_table(df, index='Sex', columns='Survived', values='Age', aggfunc='mean', margins=True)#aggfunction에 여러개pd.pivot_table(df, index='Sex', columns='Survived', values='Age', a.. 2025. 7. 14.