Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')
like image 686
Egor Epishin Avatar asked Feb 02 '26 13:02

Egor Epishin


1 Answers

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

like image 66
glhr Avatar answered Feb 04 '26 01:02

glhr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!