Skip to content

Commit bfa17b0

Browse files
committed
base64 examples
1 parent 5521171 commit bfa17b0

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed

doc/python/b64.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.3'
9+
jupytext_version: 1.15.2
10+
kernelspec:
11+
display_name: Python 3 (ipykernel)
12+
language: python
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.9.0
24+
plotly:
25+
description: How to format axes of 3d plots in Python with Plotly.
26+
display_as: b64
27+
language: python
28+
layout: base
29+
name: b64
30+
order: 1
31+
page_type: example_index
32+
permalink: python/b64/
33+
thumbnail: thumbnail/b64.png
34+
---
35+
36+
### Simple example showing how arrays of numbers could be passed as base64 typed array objects to plotly.js
37+
38+
```python
39+
import plotly.graph_objects as go
40+
41+
# x = [-200000 -100000 0 100000 200000]
42+
x = {'dtype': 'int32', 'bdata': 'wPL8/2B5/v8AAAAAoIYBAEANAwA='}
43+
44+
# y = [0 1 2 3 4 5 6 7 8 9]
45+
y = {'dtype': 'uint8', 'bdata': 'AAECAwQFBgcICQ=='}
46+
47+
# z = [
48+
# [ 61 -295 -765 863 932]
49+
# [-897 96 724 791 -993]
50+
# [ -95 -796 -285 381 669]
51+
# [ 985 -153 425 -40 136]
52+
# [-856 955 -871 414 996]
53+
# [ 966 607 -154 -251 -882]
54+
# [-492 -116 414 426 305]
55+
# [ 919 202 -505 300 -833]
56+
# [ 278 -152 -643 -950 -86]
57+
# [ 898 -532 608 -93 110]]
58+
z = {
59+
'dtype': 'int16',
60+
'bdata': 'PQDZ/gP9XwOkA3/8YADUAhcDH/yh/+T84/59AZ0C2QNn/6kB2P+IAKj8uwOZ/J4B5APGA18CZv8F/478FP6M/54BqgExAZcDygAH/iwBv/wWAWj/ff1K/Kr/ggPs/WACo/9uAA==', 'shape': '10, 5'
61+
}
62+
63+
fig = go.Figure(data=[go.Surface(
64+
x=x,
65+
y=y,
66+
z=z
67+
)])
68+
69+
fig.show()
70+
```
71+
72+
### Example where base64 is applied to pass values as typed array objects to plotly.js
73+
74+
```python
75+
import plotly.graph_objects as go
76+
import numpy as np
77+
from base64 import b64encode
78+
79+
def b64(arr) :
80+
return {
81+
'dtype': str(arr.dtype),
82+
'bdata': b64encode(arr).decode('ascii')
83+
}
84+
85+
np.random.seed(1)
86+
87+
N = 10000
88+
89+
x = np.random.randn(N)
90+
y = np.random.randn(N).astype('float32')
91+
z = np.random.randint(size=N, low=0, high=256, dtype='uint8')
92+
93+
fig = go.Figure(data=[go.Scatter3d(
94+
x=b64(x),
95+
y=b64(y),
96+
z=b64(z),
97+
mode='markers',
98+
opacity=0.1
99+
)])
100+
101+
fig.show()
102+
```
103+
104+
### Similar example where base64 is automatically applied to pass numpy arrays to plotly.js
105+
106+
```python
107+
import plotly.graph_objects as go
108+
import numpy as np
109+
110+
np.random.seed(1)
111+
112+
N = 10000
113+
114+
x = np.random.randn(N)
115+
y = np.random.randn(N).astype('float32')
116+
z = np.random.randint(size=N, low=0, high=256, dtype='uint8')
117+
118+
fig = go.Figure(data=[go.Scatter3d(
119+
x=x,
120+
y=y,
121+
z=z,
122+
mode='markers',
123+
opacity=0.1
124+
)])
125+
126+
fig.show()
127+
```
128+
129+
130+
### Example where base64 is applied to pass 2 dimensional values as typed array objects to plotly.js using shape in the spec
131+
132+
```python
133+
import plotly.graph_objects as go
134+
import numpy as np
135+
from base64 import b64encode
136+
137+
def b64(arr) :
138+
return {
139+
'dtype': str(arr.dtype),
140+
'bdata': b64encode(arr).decode('ascii'),
141+
'shape': None if arr.ndim == 1 else str(arr.shape)[1:-1]
142+
}
143+
144+
np.random.seed(1)
145+
146+
M = 100
147+
N = 200
148+
149+
x = np.arange(0, M, 1, 'int32')
150+
y = np.arange(0, N, 1, 'uint8')
151+
z = np.random.random([N, M])
152+
153+
fig = go.Figure(data=[go.Surface(
154+
x=b64(x),
155+
y=b64(y),
156+
z=b64(z)
157+
)])
158+
159+
fig.show()
160+
```
161+
162+
### Similar example where base64 is automatically applied to pass multi-dimensional numpy arrays to plotly.js
163+
164+
```python
165+
import plotly.graph_objects as go
166+
import numpy as np
167+
from base64 import b64encode
168+
169+
np.random.seed(1)
170+
171+
M = 100
172+
N = 200
173+
174+
x = np.arange(0, M, 1, 'int32')
175+
y = np.arange(0, N, 1, 'uint8')
176+
z = np.random.random([N, M])
177+
178+
fig = go.Figure(data=[go.Surface(
179+
x=x,
180+
y=y,
181+
z=z
182+
)])
183+
184+
fig.show()
185+
```
186+
187+
188+
```python
189+
190+
```
191+
192+
```python
193+
194+
```

0 commit comments

Comments
 (0)