LoginSignup
4
7

More than 3 years have passed since last update.

Plotlyでインタラクティブなグラフを作成する

Last updated at Posted at 2019-09-25

graph.png

今回の目的:足圧中心点の軌跡を「3次元」で「インタラクティブなグラフ」で表示し,データ流出対策として「オフライン」での提示をおこなう

グラフ作成の手順

1.ライブラリのインポート
2.読み込むパスの設定とcsvデータの変数への格納
3.subplotグラフの作成
4.表示するグラフの作成(subplotへの格納)
5.表示の大きさの変更
6.オフラインでの表示

1.ライブラリのインポート

#subplotを使用
from _plotly_future_ import v4_subplots 
from plotly.subplots import make_subplots
#今回はgraph_objsを使用
import plotly.graph_objs as go
#csvデータの読み込み
import numpy as np
import pandas as pd
#オフラインでの提示
import plotly.offline as offline

2.読み込むパスの設定とcsvデータの変数への格納

#ファイルパスの設定
path = 'ファイルパス'+'.csv'
path1 = 'ファイルパス1'+'.csv'
#csvからdataframeへデータの格納
df = pd.read_csv(path)
df1 = pd.read_csv(path1)
#dfに入れた二次元配列を一次元配列としてx,y,z軸それぞれに格納
CoPx=df[" COPx"] #Centor of Perssure x
CoPy=df[" COPy"] #Centor of Perssure y
time=df["time"] #time
CoPx1=df1[" COPx"] #Centor of Perssure x
CoPy1=df1[" COPy"] #Centor of Perssure y
time1=df1["time"] #time

3.subplotグラフの作成

#subplotの作成
fig = make_subplots(
    rows=2, cols=2,
    row_heights=[4,4],
    column_widths=[0.5, 0.5],
    specs=[[{'type': 'Scatter3D'}, {'type': 'Scatter'}],
           [{'type': 'Scatter'}, {'type': 'Scatter'}]],
    subplot_titles=(
        "3D Statokinesigram","Statokinesigram",
        "Changes in CoP movement over time","Changes in CoP movement over time"
    )
)

subplotは背景のようなものだと考えてください
背景を先に4分割し(rows,cols),それぞれに大きさ設定,グラフタイプを設定しタイトルを設定しています

4.表示するグラフの作成(subplotへの追加)とレイアウトの設定

#Add scatter3D of Sway body test with time flow
fig.add_trace(
    go.Scatter3d(
        mode='lines',
        x=CoPx,
        y=CoPy,
        z=time,
        name = path1,
        line=dict(
            color='#FF0033',
            width=2
        )
    ),
    row=1, col=1
)

fig.add_trace(
    go.Scatter3d(
        mode='lines',
        x=CoPx1,
        y=CoPy1,
        z=time1,
        name = path1,
        line=dict(
            color='#0000CC',
            width=2
        )
    ),
    row=1, col=1
)

rowとcolの位置を同じ位置にすることでグラフを重ねることができます

#Add scatter of Statokinesigrams
fig.add_trace(
    go.Scatter(
        mode='lines',
        x=CoPx,
        y=CoPy,
        name = path1,
        line=dict(
            color='#FF0033',
            width=2
        )
    ),
    row=1,col=2
)
fig.update_xaxes(title_text="CoPx", range=[-30, 30], row=1, col=2)
fig.update_yaxes(title_text="CoPy", range=[-30, 30], row=1, col=2)

#Add scatter of Statokinesigrams
fig.add_trace(
    go.Scatter(
        mode='lines',
        x=CoPx1,
        y=CoPy1,
        name = path1,
        line=dict(
            color='#0000CC',
            width=2
        )
    ),
    row=1,col=2
)
fig.update_xaxes(title_text="CoPx", range=[-30, 30], row=1, col=2)
fig.update_yaxes(title_text="CoPy", range=[-30, 30], row=1, col=2)
#Add scatter of stabilometry CoPx flow with time flow path1
fig.add_trace(
    go.Scatter(
        mode='lines',
        x=time,
        y=CoPx,
        name = path1,
        line=dict(
            color='#FF0033',
            width=2
        )
    ),
    row=2,col=1
)
fig.add_trace(
    go.Scatter(
        mode='lines',
        x=time,
        y=CoPx1,
        name = path1,
        line=dict(
            color='#0000CC',
            width=2
        )
    ),
    row=2,col=1
)
fig.update_xaxes(title_text="time", range=[0, 60], row=2, col=1)
fig.update_yaxes(title_text="CoPx", range=[-35, 35], row=2, col=1)
#Add scatter of stabilometry CoP flow with time flow path2
fig.add_trace(
    go.Scatter(
        mode='lines',
        x=time1,
        y=CoPy,
        name = path1,
        line=dict(
            color='#FF0033',
            width=2
        )
    ),
    row=2,col=2
)
fig.add_trace(
    go.Scatter(
        mode='lines',
        x=time1,
        y=CoPy1,
        name = path1,
        line=dict(
            color='#0000CC',
            width=2
        )
    ),
    row=2,col=2
)
fig.update_xaxes(title_text="time", range=[0, 60], row=2, col=2)
fig.update_yaxes(title_text="CoPy", range=[-35, 35], row=2, col=2)

fig.update_xaxesにx軸の名前,表示する範囲を設定しています
今回は視認性向上のため一つのグラフを作成するたびにそれに対するレイアウトを設定しました

5.表示の大きさの変更

fig.update_layout(
#ブラウザで表示
width=1700,height=1000
)

6.オフラインでの表示

offline.plot(fig, filename = path1,auto_open=True)

プロットするグラフの設定,HTMLで書き出すファイルの名前の設定,実行したさいに自動でブラウザを開く設定をしています

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7