- import
import plotly.express as px
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2], width=600, height=400)
# 타이틀 설정하기
#fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2], width=600, height=400, title="Title 설정하기")
fig.show()
- groupby 를 이용하여 데이터 시각화
import seaborn as sns
df = sns.load_dataset('penguins')
df['count'] = 1
df_count= df.groupby("species").sum().sort_values(by = "count", ascending =False)
fig = px.bar(x = df_count.index , y = df_count['count'])
fig.show()
df = sns.load_dataset('penguins')
df_count= df.groupby("species").size().sort_values(ascending=False)
fig = px.bar(y= df_count.values, x = df_count.index, labels= {'x': 'species', 'y': 'count'})
fig
- 타이틀 설정
import plotly.graph_objects as go
fig = go.Figure(data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])])
fig.update_layout(width=600,height=400)
fig.update_layout(title_text="Title 입력") # 타이틀 설정하기
fig.update_xaxes(title_text='X축 타이틀명') # Plotly 축 타이틀 설정하기(Axes Title)
fig.update_yaxes(title_text='Y축 타이틀명') # Plotly 축 타이틀 설정하기(Axes Title)
fig.show()
- add_trace() figure에 새로운 trace 추가
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(x=[1, 2, 3], y=[1, 3, 2]))
fig.show()
- add_trace() figure에 새로운 trace 추가 2.
import plotly.express as px
import plotly.graph_objects as go
# 데이터 불러오기
df = px.data.iris()
# express를 활용한 scatter plot 생성
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()
- LinearRegreesion() 을 활용한 데이터 예측 시각화
from sklearn.linear_model import LinearRegression
tip= sns.load_dataset("tips")
tip['tip_pct'] = tip['tip'] / tip['total_bill']
fig = px.scatter(tip, x = "total_bill", y ="tip_pct")
fig
linear_regression = LinearRegression()
# fit의 x데이터는 2차원으로 할당해야함
linear_regression.fit(tip[['total_bill']], tip["tip_pct"])
# predict도 마찬가지임
prediction = linear_regression.predict(tip[['total_bill']])
fig.add_trace(go.Scatter(x=tip['total_bill'], y= prediction,
mode='lines',
name='lines'))
- groupby 를 이용한 바 그래프
import seaborn as sns
df = sns.load_dataset('penguins')
df['count'] = 1
df_count= df.groupby(["species", 'sex'])['count'].sum().reset_index()
fig = px.bar(df_count, x ="species" ,color = 'sex', y = "count" ,barmode = 'group')
fig.show()
- hover 설정
import plotly.express as px
#데이터 불러오기
df = px.data.gapminder().query("continent=='Oceania'")
#그래프 그리기
fig = px.line(df, x="year", y="lifeExp", color="country")
fig.update_traces(mode="markers+lines")
# Hover 설정 X 축으로 바꾸
fig.update_layout(hovermode="x")
# Hover 스타일 편집하기
fig.update_layout(
hoverlabel_bgcolor="white",
hoverlabel_font_size=20,
hoverlabel_font_color='red',
hoverlabel_font_family="Rockwell")
fig.show()
호버 매개변수 설정
import seaborn as sns
flights = sns.load_dataset("flights")
#그래프 그리기
fig = px.line(flights, x="year", y="passengers", color="month")
fig.update_traces(mode="markers+lines")
# Hover 설정 X 축으로 바꾸
fig.update_layout(hovermode="x")
# Hover 스타일 편집하기
fig.update_layout(
hoverlabel_bgcolor="white",
hoverlabel_font_size=20,
hoverlabel_font_color='red',
hoverlabel_font_family="Rockwell")
fig.show()
- express의 선형회귀 사용하기
import plotly.express as px
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", trendline="ols")
fig.show()
# trendline = "ols"
- 극좌표
import plotly.express as px
df = px.data.wind()
df
fig = px.scatter_polar(df, r="frequency", theta="direction")
fig.show()
- subplot()
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=3, cols=1)
fig.append_trace(go.Scatter(
x=[3, 4, 5],
y=[1000, 1100, 1200],
), row=1, col=1)
fig.append_trace(go.Scatter(
x=[2, 3, 4],
y=[100, 110, 120],
), row=2, col=1)
fig.append_trace(go.Scatter(
x=[0, 1, 2],
y=[10, 11, 12]
), row=3, col=1)
fig.update_layout(height=600, width=600, title_text="Stacked Subplots")
fig.show()
나머지 여기 참고