Plotly 简明教程
Plotly - Environment Setup
本章重点介绍了如何在 Plotly 的帮助下完成 Python 中的环境设置。
This chapter focusses on how to do the environmental set up in Python with the help of Plotly.
Installation of Python package
强烈建议使用 Python 的虚拟环境功能来安装新包。以下命令在指定的文件夹中创建了一个虚拟环境。
It is always recommended to use Python’s virtual environment feature for installation of a new package. Following command creates a virtual environment in the specified folder.
python -m myenv
要激活创建的虚拟环境,请在 bin 子文件夹中运行 activate 脚本,如下所示。
To activate the so created virtual environment run activate script in bin sub folder as shown below.
source bin/activate
现在,我们可以使用 pip 实用工具如下安装 plotly 的 Python 包。
Now we can install plotly’s Python package as given below using pip utility.
pip install plotly
你可能还希望安装 Jupyter notebook 应用程序,该应用程序是 Ipython 解释器的基于 Web 的接口。
You may also want to install Jupyter notebook app which is a web based interface to Ipython interpreter.
pip install jupyter notebook
首先,你需要在以下网站上创建一个帐户: https://plot.ly 。你可以使用此处提及的链接 https://plot.ly/api_signup 注册,然后成功登录。
Firstly, you need to create an account on website which is available at https://plot.ly. You can sign up by using the link mentioned herewith https://plot.ly/api_signup and then log in successfully.
接下来,从仪表板的设置页面获取 API 密钥。
Next, obtain the API key from settings page of your dashboard.
使用你的用户名和 API 密钥设置 Python interpreter 会话中的凭据。
Use your username and API key to set up credentials on Python interpreter session.
import plotly
plotly.tools.set_credentials_file(username='test',
api_key='********************')
一个名为 credentials 的特殊文件在你的主目录下的 .plotly subfolder 中创建。它看起来类似于以下内容:
A special file named credentials is created in .plotly subfolder under your home directory. It looks similar to the following −
{
"username": "test",
"api_key": "********************",
"proxy_username": "",
"proxy_password": "",
"stream_ids": []
}
为了生成图表,我们需要从 plotly 包中导入以下模块:
In order to generate plots, we need to import the following module from plotly package −
import plotly.plotly as py
import plotly.graph_objs as go
plotly.plotly module 包含有助于与 Plotly 服务器通信的函数。 plotly.graph_objs module 中的函数生成图形对象
plotly.plotly module contains the functions that will help us communicate with the Plotly servers. Functions in plotly.graph_objs module generates graph objects