Plotly.js已经开源了,可以在线下生成独立的html文件,因此现在通过python使用plotly完全免费了。本文介绍基于python使用plotly的方法。

Plotly for Python is now entirely open source, free, and self-hosted

安装方法

pip install plotly

需要升级的话,可以输入:

pip install plotly –upgrade

使用方法

最简单的使用方法是参照plotly网站给出的案例,见这里(https://plot.ly/python/

Initialization for Online Plotting

如果不上传到plotly网站,此处可以忽略。

import plotly
plotly.tools.set_credentials_file(username='your-name', api_key='your-pass-word')

Initialization for Offline Plotting

  • Use plotly.offline.plot() to create and standalone HTML that is saved locally and opened inside your web browser.
  • Use plotly.offline.iplot() when working offline in a Jupyter Notebook to display the plot in the notebook.

数据准备

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])

plotly.offline.plot

plotly.offline.plot(data, filename = '/Users/chengjun/GitHub/cjc2016/vis/basic-line.html')

plotly

cheatsheet

更多简单的使用参见:
https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf

Plotly Python resources:

  • Plotly matplotlib figure converter — https://plot.ly/matplotlib/getting-started/
  • Using Plotly with Python offline — https://plot.ly/python/offline/
  • Saving static images (PNG, PDF, etc) — https://plot.ly/python/static-image-export/
  • Creating HTML or PDF reports in Python — https://plot.ly/python/#report-generation
  • Security & Plotly’s server at your company — https://plot.ly/products/on-premise/
  • Creating dashboards with Plotly and Python — https://plot.ly/python/dashboard/
  • Connecting to databases — https://plot.ly/python/#databases
  • Plotly and IPython / Jupyter notebook — https://plot.ly/ipython-notebooks/

Updated:

Leave a Comment

Show Comments