Plotly-subplots-and-inset-plots

提供:Dev Guides
移動先:案内検索

Plotly-サブプロットとインセットプロット

ここでは、Plotlyのサブプロットとインセットプロットの概念を理解します。

サブプロットの作成

データの異なるビューを並べて比較すると便利な場合があります。 これはサブプロットの概念をサポートします。 * plotly.toolsモジュール*に* make_subplots()*関数を提供します。 この関数は、Figureオブジェクトを返します。

次のステートメントは、1つの行に2つのサブプロットを作成します。

fig = tools.make_subplots(rows = 1, cols = 2)

これで、図に2つの異なるトレース(上の例ではexpとlogトレース)を追加できます。

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)

図のレイアウトは、*タイトル、幅、高さ*などを指定することでさらに構成されます。 * update()*メソッドを使用します。

fig['layout'].update(height = 600, width = 800s, title = 'subplots')

これが完全なスクリプトです-

from plotly import tools
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode(connected = True)
import numpy as np
x = np.arange(1,11)
y1 = np.exp(x)
y2 = np.log(x)
trace1 = go.Scatter(
   x = x,
   y = y1,
   name = 'exp'
)
trace2 = go.Scatter(
   x = x,
   y = y2,
   name = 'log'
)
fig = tools.make_subplots(rows = 1, cols = 2)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 2)
fig['layout'].update(height = 600, width = 800, title = 'subplot')
iplot(fig)

これはプロットグリッドの形式です:[(1,1)x1、y1] [(1,2)x2、y2]

サブプロットの作成

インセットプロット

サブプロットをインセットとして表示するには、そのトレースオブジェクトを設定する必要があります。 最初に、インセットトレースの xaxis およびyaxisプロパティをそれぞれ ‘x2’ および ‘y2’ にトレースします。 次のステートメントは、 ‘log’ トレースを挿入図に挿入します。

trace2 = go.Scatter(
   x = x,
   y = y2,
   xaxis = 'x2',
   yaxis = 'y2',
   name = 'log'
)

次に、インセットのx軸とy軸の位置が主軸に対する位置を指定する domain プロパティによって定義されるLayoutオブジェクトを構成します。

xaxis2=dict(
   domain = [0.1, 0.5],
   anchor = 'y2'
),
yaxis2 = dict(
   domain = [0.5, 0.9],
   anchor = 'x2'
)

インセットにログトレースを表示し、主軸にexpトレースを表示する完全なスクリプトを以下に示します-

trace1 = go.Scatter(
   x = x,
   y = y1,
   name = 'exp'
)
trace2 = go.Scatter(
   x = x,
   y = y2,
   xaxis = 'x2',
   yaxis = 'y2',
   name = 'log'
)
data = [trace1, trace2]
layout = go.Layout(
   yaxis = dict(showline = True),
   xaxis2 = dict(
      domain = [0.1, 0.5],
      anchor = 'y2'
   ),
   yaxis2 = dict(
      showline = True,
      domain = [0.5, 0.9],
      anchor = 'x2'
   )
)
fig = go.Figure(data=data, layout=layout)
iplot(fig)

出力は以下に記載されています-

インセットプロット