본문 바로가기
Python_Wiki/Python_Library

Plotly 기초 / 스타일 설정하기

by yj-data 2025. 7. 14.

#주요 시각화 라이브러리

#matplotlib: standard, seaborn: advanced version of matplotlib, plotly: new and interactive viz lib

import plotly.express as px

fig = 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...

#example
fig = px.bar(df_groupby, x='species', y='body_mass_g')
fig.show()
#plotly lib import
import plotly.express as px

corr=doc.corr(numeric_only = True)

fig = px.imshow(
    corr,                            #계산된 상관계수 객체
    text_auto = True,                #각 셀에 상관계수 값 표시
    color_continuous_scale = 'Blues' #색상 스케일 지정
    zmin = -1, zmax = 1,             #상관계수 범위(-1~1)
    width = 800, height = 600        #그래프의 가로, 세로 크기
)

fig.show()                           #생성된 그래프 객체를 화면에 출력

 

#그래프가 보여지지 않을 경우 다음 실행 후 그래프 코드 실행

import plotly.graph_objects as go
import plotly.offline as pyo      #jupyter notebook에서 보여지도록 설정하는 부분
pyo.init_notebook_mode()

스타일 설정하기

template=템플릿명 #종류: 'ggplot2', 'seaborn', 'simple_white', 'plotly', 'plotly_white', 'plotly_dark'
color_discrete_sequence = 컬러맵명 #범주형 데이터
color_continuous_scale= 컬러맵명 #연속형 데이터

#컬러맵(색 연속) 전체 뽑아보기
fig = px.colors.sequential.swatches_continuous()
fig.show()

#컬러맵(단색) 전체 뽑아보기
fig = px.colors.qualitative.swatches()
fig.show()
#템플릿 적용
for temp in ['ggplot2', 'seaborn', 'simple_white', 'plotly', 'plotly_white', 'plotly_dark']:
    fig = px.bar(data_frame=df_groupby1, x='island', y='body_mass_g', color='sex', barmode='group', text_auto='.0d', width=700, height=500, title=f'템플릿: {temp}', labels=dict(body_mass_g='몸무게(g)', island='', sex='성별'), template=temp)
    fig.show()

 

html 파일로 저장하기

fig.write_html(파일경로 및 파일명)

fig = px.scatter(data_frame=df, x='bill_length_mm', y='bill_depth_mm', color='flipper_length_mm', width=700, height=500, color_continuous_scale=px.colors.sequential.PuBuGn, template='plotly_white')
fig.show()
fig.write_html('test.html')
#구글드라이브 마운트하고 경로 지정했으면, 그 위치에 test.html이 저장됨

'Python_Wiki > Python_Library' 카테고리의 다른 글

pandas: 시각화  (0) 2025.07.15
pandas: read_excel  (0) 2025.07.15
matplotlib / seaborn 고화질 설정  (0) 2025.07.14
matplotlib / seaborn 한글 적용하기  (0) 2025.07.14
Seaborn 스타일 설정  (2) 2025.07.14