Plotly 简明教程

Plotly - Subplots and Inset Plots

在此,我们将理解 Plotly 中子图和插图的概念。

Here, we will understand the concept of subplots and inset plots in Plotly.

Making Subplots

有时候,并排比较数据的不同视图很有用。它支持子图的概念。它在 plotly.tools module 中提供 make_subplots() 函数。该函数返回一个 Figure 对象。

Sometimes it is helpful to compare different views of data side by side. This supports the concept of subplots. It offers make_subplots() function in plotly.tools module. The function returns a Figure object.

以下语句在一个行中创建两个子图。

The following statement creates two subplots in one row.

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

现在,我们可以将两个不同的轨迹(上面的示例中的 exp 和 log 轨迹)添加到该图形。

We can now add two different traces (the exp and log traces in example above) to the figure.

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

图表的布局通过使用 update() 方法指定 title, width, height, 等进一步配置。

The Layout of figure is further configured by specifying title, width, height, etc. using update() method.

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

完整脚本如下−

Here’s the complete script −

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]

This is the format of your plot grid: [ (1,1) x1,y1 ] [ (1,2) x2,y2 ]

making subplots

Inset Plots

若要将子图显示为插图,我们需要配置其轨迹对象。首先,将插图轨迹的 xaxis 和 yaxis 属性分别配置为 ‘x2’‘y2’ 。以下语句将 ‘log’ 轨迹放入插图。

To display a subplot as inset, we need to configure its trace object. First the xaxis and yaxis properties of inset trace to ‘x2’ and ‘y2’ respectively. Following statement puts ‘log’ trace in inset.

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

其次,配置布局对象,其中插图的 x 和 y 轴位置由 domain 属性定义,该属性指定其与主轴相关的位置。

Secondly, configure Layout object where the location of x and y axes of inset is defined by domain property that specifies is position with respective to major axis.

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

在插图中显示 log 轨迹并在主轴上显示 exp 轨迹的完整脚本如下:

Complete script to display log trace in inset and exp trace on main axis is given below −

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)

输出如下:

The output is mentioned below −

inset plots