You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notebooks/static-image-export.md
+50-83Lines changed: 50 additions & 83 deletions
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,21 @@ jupyter:
6
6
extension: .md
7
7
format_name: markdown
8
8
format_version: '1.1'
9
-
jupytext_version: 1.1.1
9
+
jupytext_version: 1.1.6
10
10
kernelspec:
11
-
display_name: Python 2
11
+
display_name: Python 3
12
12
language: python
13
-
name: python2
13
+
name: python3
14
+
language_info:
15
+
codemirror_mode:
16
+
name: ipython
17
+
version: 3
18
+
file_extension: .py
19
+
mimetype: text/x-python
20
+
name: python
21
+
nbconvert_exporter: python
22
+
pygments_lexer: ipython3
23
+
version: 3.7.3
14
24
plotly:
15
25
description: Plotly allows you to save static images of your plots. Save the image
16
26
to your local computer, or embed it inside your Jupyter notebooks as a static
@@ -26,101 +36,82 @@ jupyter:
26
36
permalink: python/static-image-export/
27
37
thumbnail: thumbnail/static-image-export.png
28
38
title: Static Image Export | plotly
39
+
v4upgrade: true
29
40
---
30
41
31
-
#### New to Plotly?
32
-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
33
-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
34
-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
35
-
36
-
### Version Check
37
-
Note: The static image export API is available in version <b>3.2.0.+</b><br>
38
-
39
-
```python
40
-
import plotly
41
-
42
-
plotly.__version__
43
-
```
44
-
45
42
<!-- #region -->
46
43
### Static Image Export
47
-
New in version 3.2.0. It's now possible to programmatically export figures as high quality static images while fully offline.
44
+
It's possible to programmatically export figures as high quality static images while fully offline.
48
45
49
46
#### Install Dependencies
50
-
Static image generation requires the [orca](https://github.com/plotly/orca) commandline utility and the [psutil](https://github.com/giampaolo/psutil) Python library. There are 3 general approach to installing these dependencies.
47
+
Static image generation requires the [orca](https://github.com/plotly/orca) commandline utility and the [psutil](https://github.com/giampaolo/psutil)and [requests](https://2.python-requests.org/en/master/)Python libraries. There are 3 general approach to installing these dependencies.
51
48
52
49
##### conda
53
50
Using the [conda](https://conda.io/docs/) package manager, you can install these dependencies in a single command:
**Note:** Even if you don't want to use conda to manage your Python dependencies, it is still useful as a cross platform tool for managing native libraries and command-line utilities (e.g. git, wget, graphviz, boost, gcc, nodejs, cairo, etc.). For this use-case, start with [Miniconda](https://conda.io/miniconda.html) (~60MB) and tell the installer to add itself to your system `PATH`. Then run `conda install plotly-orca` and the orca executable will be available system wide.
55
+
**Note:** Even if you do not want to use conda to manage your Python dependencies, it is still useful as a cross platform tool for managing native libraries and command-line utilities (e.g. git, wget, graphviz, boost, gcc, nodejs, cairo, etc.). For this use-case, start with [Miniconda](https://conda.io/miniconda.html) (~60MB) and tell the installer to add itself to your system `PATH`. Then run `conda install plotly-orca` and the orca executable will be available system wide.
59
56
60
57
##### npm + pip
61
58
You can use the [npm](https://www.npmjs.com/get-npm) package manager to install `orca` (and its `electron` dependency), and then use pip to install `psutil`:
62
59
63
60
```
64
61
$ npm install -g electron@1.8.4 orca
65
-
$ pip install psutil
62
+
$ pip install psutil requests
66
63
```
67
64
68
65
##### Standalone Binaries + pip
69
66
If you are unable to install conda or npm, you can install orca as a precompiled binary for your operating system. Follow the instructions in the orca [README](https://github.com/plotly/orca) to install orca and add it to your system `PATH`. Then use pip to install `psutil`.
70
67
71
68
```
72
-
$ pip install psutil
69
+
$ pip install psutil requests
73
70
```
74
71
<!-- #endregion -->
75
72
76
73
### Create a Figure
77
74
Now let's create a simple scatter plot with 100 random points of variying color and size.
78
75
79
76
```python
80
-
from plotly.offline import iplot, init_notebook_mode
81
77
import plotly.graph_objs as go
82
-
import plotly.io as pio
83
-
84
-
import os
85
78
import numpy as np
86
-
```
87
-
88
-
We'll configure the notebook for use in [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode
89
-
90
-
```python
91
-
init_notebook_mode(connected=True)
92
-
```
93
79
94
-
```python
95
80
N =100
96
81
x = np.random.rand(N)
97
82
y = np.random.rand(N)
98
83
colors = np.random.rand(N)
99
-
sz = np.random.rand(N)*30
84
+
sz = np.random.rand(N)*30
100
85
101
86
fig = go.Figure()
102
-
fig.add_scatter(x=x,
103
-
y=y,
104
-
mode='markers',
105
-
marker={'size': sz,
106
-
'color': colors,
107
-
'opacity': 0.6,
108
-
'colorscale': 'Viridis'
109
-
})
110
-
iplot(fig)
87
+
fig.add_trace(go.Scatter(
88
+
x=x,
89
+
y=y,
90
+
mode="markers",
91
+
marker=go.scatter.Marker(
92
+
size=sz,
93
+
color=colors,
94
+
opacity=0.6,
95
+
colorscale="Viridis"
96
+
)
97
+
))
98
+
99
+
fig.show()
111
100
```
112
101
113
102
### Write Image File
114
-
The `plotly.io.write_image` function is used to write an image to a file or file-like python object.
103
+
The `plotly.io.write_image` function is used to write an image to a file or file-like python object. You can also use the `.write_image` graph object figure method.
115
104
116
105
Let's first create an output directory to store our images
117
106
118
107
```python
119
-
ifnot os.path.exists('images'):
120
-
os.mkdir('images')
108
+
import os
109
+
110
+
ifnot os.path.exists("images"):
111
+
os.mkdir("images")
121
112
```
122
113
123
-
If you are running this notebook live, click to [open the output directory](./images) so you can examine the images as they're written.
114
+
If you are running this notebook live, click to [open the output directory](./images) so you can examine the images as they are written.
124
115
125
116
126
117
#### Raster Formats: PNG, JPEG, and WebP
@@ -129,19 +120,19 @@ If you are running this notebook live, click to [open the output directory](./im
129
120
Orca can output figures to several raster image formats including **PNG**, ...
Orca can also output figures in several vector formats including **SVG**, ...
151
142
152
143
```python
153
-
pio.write_image(fig, 'images/fig1.svg')
144
+
fig.write_image("images/fig1.svg")
154
145
```
155
146
156
147
**PDF**, ...
157
148
158
149
```python
159
-
pio.write_image(fig, 'images/fig1.pdf')
150
+
fig.write_image("images/fig1.pdf")
160
151
```
161
152
162
153
and **EPS** (requires the poppler library)
163
154
164
155
```python
165
-
pio.write_image(fig, 'images/fig1.eps')
156
+
fig.write_image("images/fig1.eps")
166
157
```
167
158
168
159
**Note:** It is important to note that any figures containing WebGL traces (i.e. of type `scattergl`, `heatmapgl`, `contourgl`, `scatter3d`, `surface`, `mesh3d`, `scatterpolargl`, `cone`, `streamtube`, `splom`, or `parcoords`) that are exported in a vector format will include encapsulated rasters, instead of vectors, for some parts of the image.
169
160
170
161
171
162
### Get Image as Bytes
172
-
The `plotly.io.to_image` function is used to return an image as a bytes object.
163
+
The `plotly.io.to_image` function is used to return an image as a bytes object. You can also use the `.to_image` graph object figure method.
173
164
174
165
Let convert the figure to a **PNG** bytes object...
175
166
176
167
```python
177
-
img_bytes =pio.to_image(fig, format='png')
168
+
img_bytes =fig.to_image(format="png")
178
169
```
179
170
180
171
and then display the first 20 bytes.
@@ -195,35 +186,11 @@ Image(img_bytes)
195
186
In addition to the image format, the `to_image` and `write_image` functions provide arguments to specify the image `width` and `height` in logical pixels. They also provide a `scale` parameter that can be used to increase (`scale` > 1) or decrease (`scale` < 1) the physical resolution of the resulting image.
In summary, to export high-quality static images from plotly.py all you need to do is install orcaand psutil and then use the `plotly.io.write_image` and `plotly.io.to_image` functions.
194
+
In summary, to export high-quality static images from plotly.py, all you need to do is install orca, psutil, and requests and then use the `plotly.io.write_image` and `plotly.io.to_image` functions (or the `.write_image` and `.to_image` graph object figure methods).
204
195
205
196
If you want to know more about how the orca integration works, or if you need to troubleshoot an issue, please check out the [Orca Management](../orca-management/) section.
'Plotly allows you to save static images of your plots. Save the image to your local computer, or embed it inside your Jupyter notebooks as a static image.',
0 commit comments