一 Plotly 簡介
Plotly是另一個免費進行數據分析和繪制圖表的APP,建立在d3.js上。 Plotly圖可下載為SVG,EPS或PNG格式,並簡單地導入到Illustrator或者Photoshop中。詳細內容可以去官網參考:
https://plot.ly/python/
1 安裝
安裝Plotly的Python安裝包,需要使用pip命令來安裝,安裝命令如下。
sudo pip install plotly
如果安裝成功會下圖所示。
如果要升級Plotly模塊,使用以下命令來升級。
pip install plotly --upgrade
2 初始化在線繪圖
Plotly提供了在線服務,首先需要去以下的官網去注冊一個賬號
https://plot.ly/ssu/
然后在線生成一個私鑰 API Key, 這里可以使用我已經注冊的賬戶和私鑰。
賬戶: templarz
API Key : PtKMjV9gAzINZqmQRU4T
在python交互環境下運行以下初始化命令,生成認證的安全文件 .plotly/.credentials。
import plotly plotly.tools.set_credentials_file(username='templarz', api_key='PtKMjV9gAzINZqmQRU4T')
初始化生成的Plotly的安全文件.plotly/.credentials在home目錄下,在~/.plotly/.credentials下。在文件中會看到以下內容,就說明初始化命令起作用了。
{ "username": "templarz", "stream_ids": [], "api_key": "PtKMjV9gAzINZqmQRU4T", "proxy_username": "", "proxy_password": "" }~
注意:
文中使用的開發環境是Ubuntu,如果是在Windows環境生成的安全文件,那么它所在的位置會有所不同。
3 例子1 生成在線折線圖
當使用Plotly web服務時,生成的plot和數據會保存到你的雲賬戶中,下面的例子運行后會生成一個獨一無二的url地址,通過這個地址可以訪問你生成的plot圖形。
import plotly.plotly as py from plotly.graph_objs import * trace0 = Scatter( x=[1, 2, 3, 4], y=[10, 15, 13, 17] ) trace1 = Scatter( x=[1, 2, 3, 4], y=[16, 5, 11, 9] ) data = Data([trace0, trace1]) py.plot(data, filename = 'basic-line')
運行以上那段代碼后,會生成在瀏覽器中打開地址https://plot.ly/%7Etemplarz/4/ 地址,在雲賬戶templarz中顯示生成的plot圖形如下圖所示。
使用以下命令查看py.plot的api使用方法。
import plotly.plotly as py help(py.plot)
輸入后得到以下消息提示。
Help on function plot in module plotly.plotly.plotly: plot(figure_or_data, validate=True, **plot_options) Create a unique url for this plot in Plotly and optionally open url. plot_options keyword agruments: filename (string) -- the name that will be associated with this figure fileopt ('new' | 'overwrite' | 'extend' | 'append') -- 'new' creates a 'new': create a new, unique url for this plot 'overwrite': overwrite the file associated with `filename` with this 'extend': add additional numbers (data) to existing traces 'append': add additional traces to existing data lists auto_open (default=True) -- Toggle browser options True: open this plot in a new browser tab False: do not open plot in the browser, but do return the unique url sharing ('public' | 'private' | 'secret') -- Toggle who can view this graph - 'public': Anyone can view this graph. It will appear in your profile and can appear in search engines. You do not need to be logged in to Plotly to view this chart. - 'private': Only you can view this plot. It will not appear in the Plotly feed, your profile, or search engines. You must be logged in to Plotly to view this graph. You can privately share this graph with other Plotly users in your online Plotly account and they will need to be logged in to view this plot. - 'secret': Anyone with this secret link can view this chart. It will not appear in the Plotly feed, your profile, or search engines. If it is embedded inside a webpage or an IPython notebook, anybody who is viewing that page will be able to view the graph. You do not need to be logged in to view this plot. world_readable (default=True) -- Deprecated: use "sharing". Make this figure private/public >>>
資料參考:
https://plot.ly/python/getting-started/
http://www.cnblogs.com/chaosimple/p/4153083.html