import plotly.express as px
#데이터 불러오기
df = px.data.iris()
# Figure 생성
fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")
# 눈금 생성
fig.update_xaxes(ticks="outside")#눈금이 x좌료의 바깥쪽에 생선된다
fig.update_yaxes(ticks="inside")#눈금이 y좌료의 안쪽에 생성된다
fig.show()
import plotly.express as px
#데이터 불러오기
df = px.data.iris()
# Figure 생성
fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")
#facet_col-:"species" 는 종으로 나뉘기 때문에 3개가 생성됩니다
# 눈금 생성
fig.update_xaxes(ticks="outside")
fig.update_yaxes(ticks="inside", col=1)#y좌표의 생선된것중 col1만 눈금을 생성합니다
fig.show()
fig.update_xaxes(dtick=간격 입력)
fig.update_yaxes(dtick=간격 입력)#원하는 만큼 간격을 조절할 수 있습니다
fig.update_xaxes(tickvals=[tick 좌료 리스트])
fig.update_yaxes(tickvals=[tick 좌료 리스트])#자동생성이 아닌 생성된 위치 값을 입력하여 제한한다
#예제
fig.update_yaxes(tickvals=[5.1, 5.9, 6.3, 7.5])# 이러면 y축의 5.1, 5.9, 6.3, 7.5에 눈금이 생성된다
fig.update_xaxes(tickwidth=두꺠, tickcolor=색, ticklen=길이)
fig.update_yaxes(tickwidth=두꺠, tickcolor=색, ticklen=길이)#눈금의 두께, 색, 길이를 조절할 수 있다
fig.update_yaxes(ticklabelposition="위치")
fig.update_xaxes(ticklabelposition="위치")#레이블의 위치를 바꿀수 있다
#위치 {"outside" | "inside" | "outside top" | "inside top" | "outside left" | "inside left" | "outside right" | "inside right" | "outside bottom" | "inside bottom"}
fig.update_xaxes(showticklabels=False)#레이블을 비활성화 삭제 시켜준다
fig.update_yaxes(showticklabels=False)
import plotly.express as px
#데이터 불러오기
df = px.data.tips()
# Figure 생성
fig = px.histogram(df, x="sex", y="tip", histfunc='sum', facet_col='smoker')
#담배 피는 사람과 안피는 사람 두 종류로 분류한다
# 축 스타일 편집
fig.update_xaxes(showline=True, linewidth=3, linecolor='black')#라인을 보여주나=ture, 라인 두깨=3, 라인 색 = black
fig.update_yaxes(showline=True, linewidth=3, linecolor='red')
#이것도 뒤에 , col을 붙여주면 원하는 것만 지정해서 레이블을 달아 줄 수 있다
fig.show()
fig.update_xaxes(mirror=True)
fig.update_yaxes(mirror=True)#거울 보듯이 똑같이 라인의 스타일을 맞추냐 True False
그리드는 총 2종류가 있습니다. 큰 단위로 나뉘주는 그리드와 더 작은 단위로 촘촘하게 나뉘주는 minor gird가 있습니다. 기본 그리드는 설정을 따로 하지 않아도 시각화 시 자동으로 보여집니다. minor grid는 따로 코드를 추가해야만 추가가 됩니다.
import plotly.express as px
#데이터 불러오기
df = px.data.iris()
# Figure 생성
fig = px.scatter(df, x="sepal_width", y="sepal_length", facet_col="species")
# 그리드/ minor 그리드 시각화
fig.update_xaxes(showgrid=True, minor_showgrid=True)
fig.update_yaxes(showgrid=True, minor_showgrid=True)
#그리드 또한 ,col을 사용하여 원하는 곳에만 지정할 수 있습니다
fig.show()
# 그리드 스타일 편집
fig.update_xaxes(gridwidth= 두깨, gridcolor= 색,griddash = 그리드 모양)
fig.update_yaxes(gridwidth= 두깨, gridcolor= 색,griddash = 그리드 모양)
# minor 그리드 스타일 편집
fig.update_xaxes(minor_gridwidth= 두깨, minor_gridcolor= 색,minor_griddash = 그리드 모양)
fig.update_yaxes(minor_gridwidth= 두깨, minor_gridcolor= 색,minor_griddash = 그리드 모양)
1.Base 그래프 그리기’
import plotly.graph_objects as go
# Base Figure 생성
fig = go.Figure()
fig.show()
2.추가 할 그래프 그리기
import plotly.graph_objects as go
#데이터 생성
import numpy as np
np.random.seed(1)4
N = 100
random_x = np.linspace(0, 1, N)#0~1까지 N값을 랜덤으로 뽑는다
random_y0 = np.random.randn(N) + 5#n값 +5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N) - 5#n값 -5 하는 이유는 그래프들이 겹치지 않기 위해서
# Base Figure 생성
fig = go.Figure()
# 추가 할 그래프 그리기
fig.add_trace(go.Scatter(x=random_x, y=random_y0,
mode='lines',
name='lines'))
fig.add_trace(go.Scatter(x=random_x, y=random_y1,
mode='lines+markers',
name='lines+markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y2,
mode='markers',
name='markers'))
fig.show()
from plotly.subplots import make_subplots
import plotly.graph_objects as go
# 나눠서 그릴 공간 생성
fig = make_subplots(rows=1, cols=2)
# 각 공간에 Trace 채워넣
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),
row=1, col=1
)
fig.add_trace(
go.Scatter(x=[20, 30, 40], y=[50, 60, 70]),
row=1, col=2
)
fig.show()
subplot_titles=("Plot 1", "Plot 2", "Plot 3", "Plot 4"))
#자동으로 이름을 달아준다
column_widths=[0.7, 0.3], row_heights =[0.7, 0.3])#각 trace가 위치할 크기 비율
shared_xaxes=(True or False), shared_yaxes=(True or False))
specs=[[{}, {}],
[{"colspan": 2}, None]],#여기 공간은 총 2행2열 이므로 2행 1열과 2헹 2열을 병합 함으로 colspan:2를 쓰고 2행2열 자리에는 None을 입력해준다
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])#secondary = True로 이중축이 활성화 되고 오른쪽에 축이 생성됨
# 표 추가
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis data"),
secondary_y=False,
)
#표 추가
fig.add_trace(
go.Scatter(x=[2, 3, 4], y=[4, 5, 6], name="yaxis2 data"),
secondary_y=True,
)
# 타이틀 추가
fig.update_layout(
title_text="Double Y Axis Example"
)
# X축 이름
fig.update_xaxes(title_text="xaxis title")
# Y축 이름
fig.update_yaxes(title_text="<b>primary</b> yaxis title", secondary_y=False)
fig.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True)
fig.show()
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create figure with secondary y-axis
fig = make_subplots(rows=2, cols=2,
specs=[[{"secondary_y": True}, {"secondary_y": True}],
[{"secondary_y": True}, {"secondary_y": True}]])
#4개의 그래프 모두 이중축을 허용한다
# Top left
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis data"),
row=1, col=1, secondary_y=False)
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis2 data"),
row=1, col=1, secondary_y=True,
)
# Top right
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis3 data"),
row=1, col=2, secondary_y=False,
)
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis4 data"),
row=1, col=2, secondary_y=True,
)
# Bottom left
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis5 data"),
row=2, col=1, secondary_y=False,
)
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis6 data"),
row=2, col=1, secondary_y=True,
)
# Bottom right
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[2, 52, 62], name="yaxis7 data"),
row=2, col=2, secondary_y=False,
)
fig.add_trace(
go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis8 data"),
row=2, col=2, secondary_y=True,
)
fig.show()