Search code examples
pythongridplotlysurface

How to modify grid dimension in plotly?


I have a list of X values, a list of Y values, and for each pair (X,Y), I have a value Z in the form of a matrix.

I want to represent it using plotly in python and modify the grid in which the surface is drawn so the x axis is larger than usual, getting the shape of a rectangle.

Consider the following example:

import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np
x = np.arange(1, 22, 1)
y = np.array([1, 3.1, 5.7, 10, 15, 20])
mean = [0]*21
cov = np.identity(21)
z = np.random.multivariate_normal(mean, cov, 18)
data = [go.Surface(x=x, y=y, z=z)]
py.plot(data)

enter image description here

Here, the x and y axis have the same length in the grid, but I would like the x axis to have double the actual length. I have been looking for a sort of scale parameter in plotly but did not find an answer.


Solution

  • You can manually set the aspect ratio of axes in the layout of the figure:

    data = [go.Surface(x=x, y=y, z=z)]
    
    layout = go.Layout(
        scene = go.layout.Scene(
        aspectmode='manual',
        aspectratio=go.layout.scene.Aspectratio(
            x=2, y=1, z=0.5
        ))
    )
    fig = go.Figure(data=data, layout=layout)
    
    py.plot(fig)
    

    This will set the X axis to be twice as long as the Y axis, and the Z axis to be half as long as the Y axis:

    Plotly output after fixing aspect ratio

    See the official Plotly examples.