Plotly 简明教程

Plotly - FigureWidget Class

Plotly 3.0.0 引入一个新的 Jupyter 小部件类: plotly.graph_objs.FigureWidget 。它具有与我们现有的图形相同的调用签名,并且专门用于 Jupyter NotebookJupyterLab environments

Plotly 3.0.0 introduces a new Jupyter widget class: plotly.graph_objs.FigureWidget. It has the same call signature as our existing Figure, and it is made specifically for Jupyter Notebook and JupyterLab environments.

go.FigureWiget() function 返回一个具有默认 x 和 y 轴的空 FigureWidget 对象。

The go.FigureWiget() function returns an empty FigureWidget object with default x and y axes.

f = go.FigureWidget()
iplot(f)

以下是代码的输出 −

Given below is the output of the code −

figure widget graph

FigureWidget 最重要的特点是生成的结果 Plotly 图形,并且当我们将数据和其他布局属性添加到它时,它会动态更新。

Most important feature of FigureWidget is the resulting Plotly figure and it is dynamically updatable as we go on adding data and other layout attributes to it.

例如,逐个添加以下图形迹线并查看原始空图形动态更新。这意味着我们不必反复调用 iplot() 函数,因为绘图会自动刷新。FigureWidget 的最终外观如下所示 -

For example, add following graph traces one by one and see the original empty figure dynamically updated. That means we don’t have to call iplot() function again and again as the plot is refreshed automatically. Final appearance of the FigureWidget is as shown below −

f.add_scatter(y = [2, 1, 4, 3]);
f.add_bar(y = [1, 4, 3, 2]);
f.layout.title = 'Hello FigureWidget'
figure widget

此小部件能够对悬停、单击和选择点以及缩放区域的事件进行监听。

This widget is capable of event listeners for hovering, clicking, and selecting points and zooming into regions.

在以下示例中,FigureWidget 被编程为响应绘图区域中的单击事件。小部件本身包含具有标记的简单散点图。鼠标单击位置用不同的颜色和大小标记。

In following example, the FigureWidget is programmed to respond to click event on plot area. The widget itself contains a simple scatter plot with markers. The mouse click location is marked with different color and size.

x = np.random.rand(100)
y = np.random.rand(100)
f = go.FigureWidget([go.Scatter(x=x, y=y, mode='markers')])

scatter = f.data[0]
colors = ['#a3a7e4'] * 100

scatter.marker.color = colors
scatter.marker.size = [10] * 100
f.layout.hovermode = 'closest'
def update_point(trace, points, selector):

c = list(scatter.marker.color)
s = list(scatter.marker.size)
for i in points.point_inds:

c[i] = 'red'
s[i] = 20

scatter.marker.color = c
scatter.marker.size = s
scatter.on_click(update_point)
f

在 Jupyter Notebook 中运行以上代码。将显示一个散点图。单击该区域中的某个位置,该位置将用红色标记。

Run above code in Jupyter notebook. A scatter plot is displayed. Click on a location in the area which will be markd with red colour.

location

Plotly 的 FigureWidget 对象还可以使用 Ipython’s 自己的小部件。此处,我们使用 ipwidgets 模块中定义的交互控制。我们首先构造一个 FigureWidget 并添加一个 empty scatter plot

Plotly’s FigureWidget object can also make use of Ipython’s own widgets. Here, we use interact control as defined in ipwidgets module. We first construct a FigureWidget and add an empty scatter plot.

from ipywidgets import interact
fig = go.FigureWidget()
scatt = fig.add_scatter()
fig

我们现在定义一个 update function ,它输入频率和相位,并设置上面定义的 scatter trace 的 x 和 y 属性。来自 ipywidgets 模块的 @interact decorator 用于创建一组简单的部件来控制绘图的参数。更新函数使用来自 ipywidgets package@interact decorator 进行装饰。装饰器参数用于指定我们要扫过的参数范围。

We now define an update function that inputs the frequency and phase and sets the x and y properties of the scatter trace defined above. The @interact decorator from ipywidgets module is used to create a simple set of widgets to control the parameters of a plot. The update function is decorated with @interact decorator from the ipywidgets package. The decorator parameters are used to specify the ranges of parameters that we want to sweep over.

xs = np.linspace(0, 6, 100)
@interact(a = (1.0, 4.0, 0.01), b = (0, 10.0, 0.01), color = ['red', 'green', 'blue'])
def update(a = 3.6, b = 4.3, color = 'blue'):
with fig.batch_update():
scatt.x = xs
scatt.y = np.sin(a*xs-b)
scatt.line.color = color

空 FigureWidget 现在填充为蓝色,其中 sine curve a 和 b 分别为 3.6 和 4.3。在当前 Notebook 单元格的下方,你将获得一组滑块,用于选择 ab 的值。还有一个下拉列表可用于选择迹线颜色。这些参数在 @interact decorator 中定义。

Empty FigureWidget is now populated in blue colour with sine curve a and b as 3.6 and 4.3 respectively. Below the current notebook cell, you will get a group of sliders for selecting values of a and b. There is also a dropdown to select the trace color. These parameters are defined in @interact decorator.

interact decorator