サブプロット

Pythonで学ぶPlotlyによるデータ可視化入門

Alex Scriven

Data Scientist

サブプロットとは?

 

  • サブプロット: グリッドに配置した小さなプロット

$$

  • 異なるプロット種別(同一データ)や異なるデータ部分を表示

 

サブプロットの概要

Pythonで学ぶPlotlyによるデータ可視化入門

トレースの復習

 

  • Plotly の図は traces(データと種類)のリストを持つ

  • fig.data[0], fig.data[1] でアクセス

    • .add_trace() でサブプロットに追加可能

$$

px_fig = px.scatter(df...)
print(px_fig)
Figure({'data': [trace1], 'layout': {...}})
Pythonで学ぶPlotlyによるデータ可視化入門

1x2 サブプロットの作成

import plotly.express as px
from plotly.subplots import make_subplots

# Create a subplot grid
fig = make_subplots(rows=2, cols=1)

# Create plotly express figures hist = px.histogram(revenues, x='Revenue') box = px.box(revenues, x='Revenue')
# Extract traces and add to subplots fig.add_trace(hist.data[0], row=1, col=1) fig.add_trace(box.data[0], row=2, col=1) fig.show()

 

ファイナンスのサブプロット

Pythonで学ぶPlotlyによるデータ可視化入門

サブプロットのカスタマイズ

 

  • 全体のタイトルなし
  • サブプロットのタイトルなし

$$

$$

$$

  • ✨ プレゼン用に仕上げる

 

シンプルなファイナンスのサブプロット

Pythonで学ぶPlotlyによるデータ可視化入門

サブプロットのタイトル

from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=1,
    subplot_titles=[
    '会社の収益のヒストグラム', 
    '会社の収益のボックスプロット'])

## Add in traces (fig.add_trace())
fig.update_layout({'title': {'text': '会社の収益のプロット', 'x': 0.5, 'y': 0.9}}) fig.show()

サブプロットのタイトルを設定

詳細はドキュメント参照

Pythonで学ぶPlotlyによるデータ可視化入門

縦に積んだサブプロット

$$

fig = make_subplots(rows=3, cols=1, 
  subplot_titles=['アデリーペンギン'
  , 'ジェンツーペンギン', 'ヒゲペンギン'])

row_num = 1 for species in ['Adelie', 'Gentoo', 'Chinstrap']: # Filter data for this species df = penguins[penguins['Species'] == species]
scatter = px.scatter(df, x='Culmen Length (mm)' , y='Culmen Depth (mm)') # Add the trace to the subplot fig.add_trace(scatter.data[0] , row=row_num, col=1) row_num +=1

ペンギンの縦積みサブプロット

Pythonで学ぶPlotlyによるデータ可視化入門

軸を共有するサブプロット

 

  • x軸を共有するには:
fig = make_subplots(
    rows=3, cols=1
    , shared_xaxes=True)

x軸共有のペンギンサブプロット

Pythonで学ぶPlotlyによるデータ可視化入門

演習に進みましょう!

Pythonで学ぶPlotlyによるデータ可視化入門

Preparing Video For Download...