import plotly.graph_objects as go
#graph_objects
fig = go.Figure(
data=[go.Bar(x=[1,2,3], y=[1,3,2])],#x는 가로 bar의 수 y= 높이 이다
layout=go.Layout(
title=go.layout.Title(text="A Figure Specified By"))#타이틀, 제목생성
)
fig.show()
#express
fig = px.bar(x=["a","b","c"], y=[1,3,2],title="A Figure Specified By express")
#여기는 데이터와 layout을 나누지 않고 px.bar로 묶어서 실행한다
fig.show()
fig = go.Figure()
fig.add_trace(go.Bar(x=[1,2,3,], y=[1,3,2]))
fig.show()
#위 막대그래프와 똑같이 출력
scatter 그래프 생성
df = px.data.iris()
fig = px.scatter(df, x= "sepal_width", y="sepal_length", color="species", title="Using The add_trace() method With A Plotly Express Figure")
fig.add_trace(
go.Scatter(
x=[2,4],
y=[4,8],
mode = "lines",
line = go.scatter.Line(color="gray"),
showlegend=False)
)
fig.show()
# subplot 생성
fig = make_subplots(rows=1, cols=2)
# Trace 추가하기
fig.add_scatter(y=[4, 2, 3.5], mode="markers",
marker=dict(size=20, color="LightSeaGreen"),
name="a", row=1, col=1)
fig.add_bar(y=[2, 1, 3],
marker=dict(color="MediumPurple"),
name="b", row=1, col=1)
fig.add_scatter(y=[2, 3.5, 4], mode="markers",
marker=dict(size=20, color="MediumPurple"),
name="c", row=1, col=2)
fig.add_bar(y=[1, 3, 2],
marker=dict(color="LightSeaGreen"),
name="d", row=1, col=2)
fig.update_traces(marker=dict(color="RoyalBlue"),
selector=dict(type="bar"))
fig.show()
update_layout() 함수를 사용하면 그래프 사이즈, 제목 및 텍스트, 글꼴크기 와 같은 Trace 외적인 그래프 요소를 업데이트 가능합니다.
#그래프 생성
fig = go.Figure(data=go.Bar(x=[1, 2, 3], y=[1, 3, 2]))
# 타이틀 추가하기
fig.update_layout(title_text="Using update_layout() With Graph Object Figures",title_font_size=30)
fig.show()
update_xaxes(), update_yaxes() 함수를 사용하면 각각 X축, Y축에 관한 다양한 편집이 가능합니다. ex) 축 타이틀, 축 라인 스타일, 그리드 설정 등
#데이터 생성
df = px.data.tips()
x = df["total_bill"]
y = df["tip"]
# 그래프 그리기
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='markers'))
# 축 타이틀 추가하기
fig.update_xaxes(title_text='Total Bill ($)')
fig.update_yaxes(title_text='Tip ($)')
fig.show()
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2],width=600, height=400)#width와 height는 그래프의 가로 세로의 크기를 정의 한다
fig.show()
fig = go.Figure(data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])])
fig.update_layout(width=600,height=400)
fig.show()
margin 이란 전체 크기(Figure) 와 그래프(Trace) 사이의 거리를 뜻합니다.
fig.update_layout(
margin_l=left margine,#왼쪽
margin_r=right margine,#오른쪽
margin_b=bottom margine,#아래
margin_t=top margine)#위
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
# 그래프 크기와 margin 설정하기
fig.update_layout(
width=600,
height=400,
margin_l=100,
margin_r=100,
margin_b=70,
margin_t=70,
# 백그라운드 칼라 지정, margin 잘 보이게 하기위함
paper_bgcolor="LightSteelBlue",
)
fig.show()