Plotly 简明教程
Plotly - Online and Offline Plotting
下一章介绍了在线和离线绘制的设置。让我们首先了解在线绘图的设置。
The following chapter deals with the settings for the online and offline plotting. Let us first study the settings for online plotting.
Settings for online plotting
Data 和 graph 联机图保存在您的 plot.ly account 中。联机图通过两种方法创建,它们两个都创建一个独特的 url 用作该图并将其保存在您的 Plotly 帐户中。
Data and graph of online plot are save in your plot.ly account. Online plots are generated by two methods both of which create a unique url for the plot and save it in your Plotly account.
-
py.plot() − returns the unique url and optionally open the url.
-
py.iplot() − when working in a Jupyter Notebook to display the plot in the notebook.
我们现在将在 radians vs. its sine value 中显示一个简单的角度图。首先,使用 numpy 库中的 arange() 函数获得 0 到 2π 之间的角度 ndarray 对象。此 ndarray 对象用作图形 x axis 上的值。需要显示在 y axis 上的角度的对应正弦值通过以下陈述获得 −
We shall now display simple plot of angle in radians vs. its sine value. First, obtain ndarray object of angles between 0 and 2π using arange() function from numpy library. This ndarray object serves as values on x axis of the graph. Corresponding sine values of angles in x which has to be displayed on y axis are obtained by following statements −
import numpy as np
import math #needed for definition of pi
xpoints = np.arange(0, math.pi*2, 0.05)
ypoints = np.sin(xpoints)
接下来,使用 graph_objs module 中的 Scatter() 函数创建一个散布轨迹。
Next, create a scatter trace using Scatter() function in graph_objs module.
trace0 = go.Scatter(
x = xpoints,
y = ypoints
)
data = [trace0]
将上述列表对象用作 plot() 函数的参数。
Use above list object as argument to plot() function.
py.plot(data, filename = 'Sine wave', auto_open=True)
将以下脚本保存为 plotly1.py
Save following script as plotly1.py
import plotly
plotly.tools.set_credentials_file(username='lathkar', api_key='********************')
import plotly.plotly as py
import plotly.graph_objs as go
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]
py.plot(data, filename = 'Sine wave', auto_open=True)
从命令行执行上述脚本。生成的图形将按如下所示在浏览器中以指定的 URL 显示。
Execute the above mentioned script from command line. Resultant plot will be displayed in the browser at specified URL as stated below.
$ python plotly1.py
High five! You successfully sent some data to your account on plotly.
View your plot in your browser at https://plot.ly/~lathkar/0

就在显示的图形上方,您将发现标签 Plot、Data、Python 和 Rand Forking History。
Just above the displayed graph, you will find tabs Plot, Data, Python & Rand Forking history.
当前, Plot tab 被选中。Data 标签显示包含 x 和 y 数据点的网格。从 Python 和 R 标签,您可以在 Python、R、JSON、Matlab 等中查看与当前图形对应的代码。以下快照显示了上面生成的图形的 Python 代码 −
Currently, Plot tab is selected. The Data tab shows a grid containing x and y data points. From Python & R tab, you can view code corresponding to current plot in Python, R, JSON, Matlab etc. Following snapshot shows Python code for the plot as generated above −

Setting for Offline Plotting
Plotly 允许您生成离线图形并将其保存在本地机器上。 plotly.offline.plot() 函数会创建独立的 HTML,将其本地保存并在您的 Web 浏览器中打开。
Plotly allows you to generate graphs offline and save them in local machine. The plotly.offline.plot() function creates a standalone HTML that is saved locally and opened inside your web browser.
在 Jupyter Notebook 中进行离线工作时,使用 plotly.offline.iplot() 在笔记本中显示图形。
Use plotly.offline.iplot() when working offline in a Jupyter Notebook to display the plot in the notebook.
Note − 离线绘图需要 Plotly 的 1.9.4+ 。
Note − Plotly’s version 1.9.4+ is needed for offline plotting.
更改脚本中的 plot() function 语句并运行。一个名为 temp-plot.html 的 HTML 文件将在本地创建并在 Web 浏览器中打开。
Change plot() function statement in the script and run. A HTML file named temp-plot.html will be created locally and opened in web browser.
plotly.offline.plot(
{ "data": data,"layout": go.Layout(title = "hello world")}, auto_open = True)
