How to set Scatter 3d animation fixed axis scale? 'fixedrange' does not work

I want to create a 3D animation, fixed perspective, and axis. But I tried many ways to fail.

My code :

from plotly.offline import plot
import plotly.graph_objs as go
import numpy as np
from tensorflow.python.keras.datasets import fashion_mnist

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
y_test = y_test[:1000]

datas = np.load('tmp/latent/his.npz', allow_pickle=True)
his = datas['arr_0'][()]  # type: dict

# ๅฎšไน‰ๅˆๅง‹ๆ•ฐๆฎ
data = [go.Scatter3d(x=his['000'][:, 0], y=his['000'][:, 1], z=his['000'][:, 2],
                     mode='markers', type='scatter3d', marker={'size': 5, 'color': y_test, 'colorscale': 'Viridis'})]
# ๅˆๅง‹ๅธƒๅฑ€
layout = go.Layout(
    xaxis={'range': [-5, 5], 'fixedrange': True, 'rangemode': 'tozero', 'tickmode': "linear", 'tick0': -5, 'dtick': 1, 'automargin': False},
    yaxis={'range': [-5, 5], 'fixedrange': True, 'rangemode': 'tozero', 'tickmode': "linear", 'tick0': -5, 'dtick': 1, 'automargin': False},
    updatemenus=[{
        'buttons': [{'args': [None], 'label':'Play', 'method':'animate'}],
        'xanchor':'center',
        'yanchor':'bottom',
        'pad':{'l': 30},
        'showactive': False,
        'type': 'buttons'}],
    scene={
        'xaxis': {'range': [-5, 5], 'rangemode': 'tozero', 'tickmode': "linear", 'tick0': -5, 'dtick': 1},
        'yaxis': {'range': [-5, 5], 'rangemode': 'tozero', 'tickmode': "linear", 'tick0': -5, 'dtick': 1},
        'zaxis': {'range': [-5, 5], 'rangemode': 'tozero', 'tickmode': "linear", 'tick0': -5, 'dtick': 1},
        'aspectratio': {
            'x': 0,
            'y': 0,
            'z': 0,
        }},
    margin={'autoexpand': False},
    autosize=False,
    width=1000,
    height=1000)


frames = [{
    'data': [go.Scatter3d(
        x=v[:, 0], y=v[:, 1], z=v[:, 2],
        mode='markers', marker={'size': 5, 'color': y_test, 'colorscale': 'Viridis'}
    )]} for i, (k, v) in enumerate(his.items())]

fig = go.Figure(data=data, layout=layout, frames=frames)
plot(fig)

Anyone can help me? Thanks :slight_smile:

For whomever else who may come here from Google, itโ€™s

scene = {
    # ...
    'aspectmode': 'cube',
}
2 Likes

Oh god Iโ€™ve been looking for this for so long thank you

For me aspectratio=dict(x=1, y=1, z=1), worked