✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

3DScatter: autorange on/off by button

Hi,
I want to control axes autoranging which is done after hiding/unhiding traces for example by clicking on legend items. In other words:

  • When “fixed Range always” option is selected, then hiding/unhiding a trace should result in “disappearing”/“appearing” from plot without changing anything else on the plot(especially axes ranges).
  • When “autorange on” option is selected, then plot should behave as it is behaving now.

My attempt to achieve this in python below:

import plotly
import plotly.graph_objs as go
import numpy as np


def minMaxRange(data, axis):
    return [min([min(plot[axis]) for plot in data]),
            max([max(plot[axis]) for plot in data])]


x, y, z = np.random.multivariate_normal(np.array([0, 0, 0]), np.eye(3), 200).transpose()
trace1 = go.Scatter3d(
    x=x,
    y=y,
    z=z,
)

x2, y2, z2 = np.random.multivariate_normal(np.array([10, 10, 10]), np.eye(3), 200).transpose()
trace2 = go.Scatter3d(
    x=x2,
    y=y2,
    z=z2,
)
data = [trace1, trace2]
updatemenus = list([
    dict(active=1,
         buttons=[
             dict(label="fixed Range always",
                  method="relayout",
                  args=[{"scene.xaxis.autorange": "false"},
                        "scene.xaxis.range", minMaxRange(data, "x"),
                        "scene.yaxis.autorange", "false",
                        "scene.yaxis.range", minMaxRange(data, "y"),
                        "scene.zaxis.autorange", "false",
                        "scene.zaxis.range", minMaxRange(data, "z"),
                        ], ),
             dict(label="autorange on",
                  method="relayout",
                  args=[{"scene.xaxis.autorange": "true"},
                        {"scene.xaxis.range": []},
                        {"scene.yaxis.autorange": "true"},
                        {"scene.yaxis.range": []},
                        {"scene.zaxis.autorange": "true"},
                        {"scene.zaxis.range": []}],
                  ),
         ]
         ),
])
layout = go.Layout(
    title="buttons should control axes autoranging, testable by hiding/unhiding traces by clicking on legend",
    updatemenus=updatemenus,
    showlegend=True,
)
fig = go.Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='simple-3d-scatter')

Plot from code above

As you can see in plot from above link my approach doesn’t work.
How to make it work in python generated plotly plot ?

Thanks a lot for answering.