Search code examples
pythonplotplotlysurface

How to center plotly 3d surface plot?


Trying to plot an intercative 3d surface with plotly (go.Surface), the resulting surface is not centered at origin. It is centered at (10,10) and axes go from 0 to 20. I want it to be centered at origin and axes be limited from -5 to 5. Would be great if someone could help me to do that.

enter image description here

x = np.arange(-5,5,0.01)
y = np.arange(-5,5,0.01)
xx,yy=np.meshgrid(x,y)
Z=xx**2+yy**2

data = [go.Surface(z=Z.tolist(), colorscale='Viridis')]

layout = go.Layout(
...
code here
...
)

fig = dict(data=data, layout=layout)

plotly.offline.plot(fig, filename='pandas-3d-surface')

Solution

  • You can specify a range for the X and Y axis like this:

    layout = go.Layout(xaxis=dict(range=[-5, 5]),
                       yaxis=dict(range=[-5, 5]))
    

    Note that this requires explicitly specifying the x and y axis in the data:

    data = [go.Surface(z=Z.tolist(), x=x, y=y, colorscale='Viridis')]
    

    Output:

    Plotly surface plot with fixed axis range for X and Y

    See the official Plotly examples