87.md 2.7 KB
Newer Older
W
wizardforcel 已提交
1
# Plotly
W
init  
wizardforcel 已提交
2 3 4 5 6

> 原文: [https://pythonbasics.org/plotly/](https://pythonbasics.org/plotly/)

Plotly 是 Python 的制图模块。 它可以创建出版物质量的图表。 它支持多种类型的图表/图表,包括折线图,条形图,气泡图等。

W
wizardforcel 已提交
7
该库是免费的开放源代码。 在本教程中,您将学习如何使用 plotly 创建折线图。 可以将其视为 Matplotlib 的更广泛的替代方案。
W
init  
wizardforcel 已提交
8

W
wizardforcel 已提交
9

W
init  
wizardforcel 已提交
10

W
wizardforcel 已提交
11
## 示例
W
init  
wizardforcel 已提交
12 13 14

### 简介

W
wizardforcel 已提交
15
从 PyPi 仓库中安装。 在新安装的虚拟环境中,您可以使用程序`pip`进行安装。
W
init  
wizardforcel 已提交
16

W
wizardforcel 已提交
17
```py
W
init  
wizardforcel 已提交
18 19 20 21 22 23 24 25
pip install plotly

```

Plotly 提供了用于绘制图表的 Web 服务。 图形将保存在您的在线 Plotly 帐户中。 这是可选的,Plotly 可以脱机使用。

离线绘图有两个选项:

W
wizardforcel 已提交
26
*   使用`plotly.offline.plot()`创建和独立的 HTML。 可以在浏览器中打开该文件
W
init  
wizardforcel 已提交
27

W
wizardforcel 已提交
28
*   在 Jupyter Notebook 中脱机工作时,请使用`plotly.offline.iplot()`
W
init  
wizardforcel 已提交
29

W
wizardforcel 已提交
30
### 在线绘图
W
init  
wizardforcel 已提交
31

W
wizardforcel 已提交
32
在线绘图需要在 [plot.ly](https://plot.ly) 上进行计费。
W
init  
wizardforcel 已提交
33 34 35

更改为您的用户名和 [API 密钥](https://plot.ly/settings/api)

W
wizardforcel 已提交
36
打开文件`~/.plotly/.credentials`并更新您的 API 密钥。
W
init  
wizardforcel 已提交
37 38 39

然后创建此程序:

W
wizardforcel 已提交
40
```py
W
init  
wizardforcel 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

py.sign_in(username='voorbeeld', api_key='L0McCrDpID71OLCEgRtK')

mx = [1, 2, 3, 4]
my = [1, 2, 3, 4]

trace = go.Scatter(
    x = mx,
        y = my
	)

data = [trace]
py.plot(data)

```

从终端运行程序。 然后打开 URL [https://plot.ly/organize/home/#/](https://plot.ly/organize/home/#/) ,您的图表将在其中显示。

![plotly plot with python](img/91a1febe058f4d82b43d642a7e9fa9bb.jpg)

W
wizardforcel 已提交
64
### 独立 HTML(`plotly.offline.plot`)
W
init  
wizardforcel 已提交
65 66 67

下面的代码创建一个新的 HTML 文件。 使用浏览器(Firefox,Chrome)打开时,此 HTML 文件将显示图表。

W
wizardforcel 已提交
68
```py
W
init  
wizardforcel 已提交
69 70 71 72 73 74 75 76 77 78
import plotly
import plotly.graph_objs as go

plotly.offline.plot({
    "data": [go.Scatter(x=[1, 2, 3, 4], y=[1, 2, 3, 4])],
        "layout": go.Layout(title="line chart")
	}, auto_open=True)

```

W
wizardforcel 已提交
79
### jupyter 笔记本
W
init  
wizardforcel 已提交
80 81 82 83 84

另一种方法是使用 jupyter 笔记本(ipython)。 ipython 是一个功能强大的交互式外壳。

您可以使用以下命令安装

W
wizardforcel 已提交
85
```py
W
init  
wizardforcel 已提交
86 87 88 89 90
python3 -m pip install jupyter
jupyter notebook

```

W
wizardforcel 已提交
91
这将启动 Web 服务器。从`/tree`页面单击“新建 -> 笔记本 -> python3”。
W
init  
wizardforcel 已提交
92 93 94

在代码框中,粘贴以下代码:

W
wizardforcel 已提交
95
```py
W
init  
wizardforcel 已提交
96 97 98 99 100 101 102 103 104 105 106 107
import plotly
import plotly.graph_objs as go

plotly.offline.init_notebook_mode(connected=True)

plotly.offline.iplot({
    "data": [go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],
        "layout": go.Layout(title="hello world")
	})

```

W
wizardforcel 已提交
108
然后单击“运行”,该图表将显示在代码下方。