Skip to content

Latest commit

 

History

History
198 lines (155 loc) · 3.86 KB

b64.md

File metadata and controls

198 lines (155 loc) · 3.86 KB
jupyter
jupytext kernelspec language_info plotly
notebook_metadata_filter text_representation
all
extension format_name format_version jupytext_version
.md
markdown
1.3
1.15.2
display_name language name
Python 3 (ipykernel)
python
python3
codemirror_mode file_extension mimetype name nbconvert_exporter pygments_lexer version
name version
ipython
3
.py
text/x-python
python
python
ipython3
3.9.0
description display_as language layout name order page_type permalink thumbnail
How to format axes of 3d plots in Python with Plotly.
b64
python
base
b64
1
example_index
python/b64/
thumbnail/b64.png

Simple example showing how arrays of numbers could be passed as base64 typed array objects to plotly.js

import plotly.graph_objects as go

# x = [-200000 -100000       0  100000  200000]
x = {'dtype': 'int32', 'bdata': 'wPL8/2B5/v8AAAAAoIYBAEANAwA='}

# y = [0 1 2 3 4 5 6 7 8 9]
y = {'dtype': 'uint8', 'bdata': 'AAECAwQFBgcICQ=='}

# z = [
# [  61 -295 -765  863  932]
# [-897   96  724  791 -993]
# [ -95 -796 -285  381  669]
# [ 985 -153  425  -40  136]
# [-856  955 -871  414  996]
# [ 966  607 -154 -251 -882]
# [-492 -116  414  426  305]
# [ 919  202 -505  300 -833]
# [ 278 -152 -643 -950  -86]
# [ 898 -532  608  -93  110]]
z = {
  'dtype': 'int16',
  'bdata': 'PQDZ/gP9XwOkA3/8YADUAhcDH/yh/+T84/59AZ0C2QNn/6kB2P+IAKj8uwOZ/J4B5APGA18CZv8F/478FP6M/54BqgExAZcDygAH/iwBv/wWAWj/ff1K/Kr/ggPs/WACo/9uAA==', 'shape': '10, 5'
}

fig = go.Figure(data=[go.Surface(
    x=x,
    y=y,
    z=z
)])

fig.show()

Example where base64 is applied to pass values as typed array objects to plotly.js

import plotly.graph_objects as go
import numpy as np
from base64 import b64encode

def b64(arr) :
  return {
      'dtype': str(arr.dtype),
      'bdata': b64encode(arr).decode('ascii')
  }

np.random.seed(1)

N = 10000

x = np.random.randn(N)
y = np.random.randn(N).astype('float32')
z = np.random.randint(size=N, low=0, high=256, dtype='uint8')
c = np.random.randint(size=N, low=-10, high=10, dtype='int8')

fig = go.Figure(data=[go.Scatter3d(
    x=b64(x),
    y=b64(y),
    z=b64(z),
    marker=dict(color= b64(c)),
    mode='markers',
    opacity=0.2
)])

fig.show()

Similar example where base64 is automatically applied to pass numpy arrays to plotly.js

import plotly.graph_objects as go
import numpy as np

np.random.seed(1)

N = 10000

x = np.random.randn(N)
y = np.random.randn(N).astype('float32')
z = np.random.randint(size=N, low=0, high=256, dtype='uint8')
c = np.random.randint(size=N, low=-10, high=10, dtype='int8')

fig = go.Figure(data=[go.Scatter3d(
    x=x,
    y=y,
    z=z,
    marker=dict(color=c),
    mode='markers',
    opacity=0.2
)])

fig.show()

Example where base64 is applied to pass 2 dimensional values as typed array objects to plotly.js using shape in the spec

import plotly.graph_objects as go
import numpy as np
from base64 import b64encode

def b64(arr) :
  return {
      'dtype': str(arr.dtype),
      'bdata': b64encode(arr).decode('ascii'),
      'shape': None if arr.ndim == 1 else str(arr.shape)[1:-1]
  }

np.random.seed(1)

M = 100
N = 200

x = np.arange(0, M, 1, 'int32')
y = np.arange(0, N, 1, 'uint8')
z = np.random.random([N, M])

fig = go.Figure(data=[go.Surface(
    x=b64(x),
    y=b64(y),
    z=b64(z)
)])

fig.show()

Similar example where base64 is automatically applied to pass multi-dimensional numpy arrays to plotly.js

import plotly.graph_objects as go
import numpy as np
from base64 import b64encode

np.random.seed(1)

M = 100
N = 200

x = np.arange(0, M, 1, 'int32')
y = np.arange(0, N, 1, 'uint8')
z = np.random.random([N, M])

fig = go.Figure(data=[go.Surface(
    x=x,
    y=y,
    z=z
)])

fig.show()