Plotly 简明教程
Plotly - Package Structure
Plotly Python 程序包包含三个主要模块,如下所示 −
Plotly Python package has three main modules which are given below −
-
plotly.plotly
-
plotly.graph_objs
-
plotly.tools
plotly.plotly module 包含需要 Plotly 服务器响应的函数。此模块中的函数是在本地机器和 Plotly 之间的接口。
The plotly.plotly module contains functions that require a response from Plotly’s servers. Functions in this module are interface between your local machine and Plotly.
plotly.graph_objs module 是最重要的模块,它包含构成所看图形的对象的所有类定义。定义了如下的图形对象 −
The plotly.graph_objs module is the most important module that contains all of the class definitions for the objects that make up the plots you see. Following graph objects are defined −
-
Figure,
-
Data,
-
ayout,
-
Different graph traces like Scatter, Box, Histogram etc.
所有图形对象都是用于生成和/或修改 Plotly 图形的每个功能的字典类和列表类对象。
All graph objects are dictionary- and list-like objects used to generate and/or modify every feature of a Plotly plot.
plotly.tools module 包含许多有助于简化和增强 Plotly 体验的函数。此模块中定义了用于 subplot generation 、在 IPython notebooks 中嵌入 Plotly 图形、保存和检索凭据的函数。
The plotly.tools module contains many helpful functions facilitating and enhancing the Plotly experience. Functions for subplot generation, embedding Plotly plots in IPython notebooks, saving and retrieving your credentials are defined in this module.
图形由 plotly.graph_objs module 中定义的 Figure 类表示的 Figure 对象表示。其构造函数需要以下参数 −
A plot is represented by Figure object which represents Figure class defined in plotly.graph_objs module. It’s constructor needs following parameters −
import plotly.graph_objs as go
fig = go.Figure(data, layout, frames)
data 参数是在 Python 中的列表对象。它是想要绘制的所有轨迹的列表。轨迹只是我们给要绘制的数据集合起的名字。 trace 对象根据想要在绘制表面上显示数据的方式命名。
The data parameter is a list object in Python. It is a list of all the traces that you wish to plot. A trace is just the name we give to a collection of data which is to be plotted. A trace object is named according to how you want the data displayed on the plotting surface.
Plotly 提供许多轨迹对象,例如 scatter, bar, pie, heatmap 等。每个轨迹都由 graph_objs 函数中的相应函数返回。例如: go.scatter() 返回散点轨迹。
Plotly provides number of trace objects such as scatter, bar, pie, heatmap etc. and each is returned by respective functions in graph_objs functions. For example: go.scatter() returns a scatter trace.
import numpy as np
import math #needed for definition of pi
xpoints=np.arange(0, math.pi*2, 0.05)
ypoints=np.sin(xpoints)
trace0 = go.Scatter(
x = xpoints, y = ypoints
)
data = [trace0]
layout 参数定义了图形的外观和与数据无关的图表功能。因此我们将能够更改标题、轴标题、注释、图例、间距、字体甚至在图表顶部绘制形状等内容。
The layout parameter defines the appearance of the plot, and plot features which are unrelated to the data. So we will be able to change things like the title, axis titles, annotations, legends, spacing, font and even draw shapes on top of your plot.
layout = go.Layout(title = "Sine wave", xaxis = {'title':'angle'}, yaxis = {'title':'sine'})
图形可以有 plot title 以及轴 title 。它也可以有注释来表示其他描述。
A plot can have plot title as well as axis title. It also may have annotations to indicate other descriptions.
最后,有一个由 go.Figure() function 创建的 Figure object 。它是一个类似字典的对象,包含数据对象和布局对象。最后,将绘制图形对象。
Finally, there is a Figure object created by go.Figure() function. It is a dictionary-like object that contains both the data object and the layout object. The figure object is eventually plotted.
py.iplot(fig)