Skip to content

Commit 82c8199

Browse files
committed
add examples using base64
1 parent 124f12d commit 82c8199

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed

Diff for: doc/python/b64.md

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
c = np.random.randint(size=N, low=-10, high=10, dtype='int8')
93+
94+
fig = go.Figure(data=[go.Scatter3d(
95+
x=b64(x),
96+
y=b64(y),
97+
z=b64(z),
98+
marker=dict(color= b64(c)),
99+
mode='markers',
100+
opacity=0.2
101+
)])
102+
103+
fig.show()
104+
```
105+
106+
### Similar example where base64 is automatically applied to pass numpy arrays to plotly.js
107+
108+
```python
109+
import plotly.graph_objects as go
110+
import numpy as np
111+
112+
np.random.seed(1)
113+
114+
N = 10000
115+
116+
x = np.random.randn(N)
117+
y = np.random.randn(N).astype('float32')
118+
z = np.random.randint(size=N, low=0, high=256, dtype='uint8')
119+
c = np.random.randint(size=N, low=-10, high=10, dtype='int8')
120+
121+
fig = go.Figure(data=[go.Scatter3d(
122+
x=x,
123+
y=y,
124+
z=z,
125+
marker=dict(color=c),
126+
mode='markers',
127+
opacity=0.2
128+
)])
129+
130+
fig.show()
131+
```
132+
133+
134+
### Example where base64 is applied to pass 2 dimensional values as typed array objects to plotly.js using shape in the spec
135+
136+
```python
137+
import plotly.graph_objects as go
138+
import numpy as np
139+
from base64 import b64encode
140+
141+
def b64(arr) :
142+
return {
143+
'dtype': str(arr.dtype),
144+
'bdata': b64encode(arr).decode('ascii'),
145+
'shape': None if arr.ndim == 1 else str(arr.shape)[1:-1]
146+
}
147+
148+
np.random.seed(1)
149+
150+
M = 100
151+
N = 200
152+
153+
x = np.arange(0, M, 1, 'int32')
154+
y = np.arange(0, N, 1, 'uint8')
155+
z = np.random.random([N, M])
156+
157+
fig = go.Figure(data=[go.Surface(
158+
x=b64(x),
159+
y=b64(y),
160+
z=b64(z)
161+
)])
162+
163+
fig.show()
164+
```
165+
166+
### Similar example where base64 is automatically applied to pass multi-dimensional numpy arrays to plotly.js
167+
168+
```python
169+
import plotly.graph_objects as go
170+
import numpy as np
171+
from base64 import b64encode
172+
173+
np.random.seed(1)
174+
175+
M = 100
176+
N = 200
177+
178+
x = np.arange(0, M, 1, 'int32')
179+
y = np.arange(0, N, 1, 'uint8')
180+
z = np.random.random([N, M])
181+
182+
fig = go.Figure(data=[go.Surface(
183+
x=x,
184+
y=y,
185+
z=z
186+
)])
187+
188+
fig.show()
189+
```
190+
191+
192+
```python
193+
194+
```
195+
196+
```python
197+
198+
```

0 commit comments

Comments
 (0)