简体   繁体   中英

Plotting a 3D surface plot in Ploty (Python) is stretched / deformed

I am plotting a two dimensional function in Plotly. I am experiencing that the plot is stretched much into the height. (see picture below). In the layout, I already changed the values for height and width, but it has no influence on it.

That is my code:

import plotly.graph_objects as go
import numpy as np


x_test_d1 = np.linspace(-4,4,100)
x_test_d2 = np.linspace(-3,3,100)


grid_d1, grid_d2 = np.meshgrid(x_test_d1,x_test_d2)

z = 1.5*np.sin(grid_d1) - 0.1*(grid_d1-3)**2 +10 - 0.5*(grid_d2**2-2) + np.sin(grid_d2)*2


fig = go.Figure(data=[go.Surface(x=grid_d1, y=grid_d2, z=z)])
fig.update_layout(margin=dict(l=20, r=20, t=20, b=20),
    width=800, height=400)

fig.show()

and that is the resulting figure:

结果图被拉伸太多:D

Thank you for any help! :)

According to documentation, you can find here

width
Code: fig.update_layout(width=)
Type: number greater than or equal to 10
Default: 700
Sets the plot's width (in px).

Somewhat counterintuitively, sets size of webview/window plot is shown in, but doesn't affect size of plot.

After a bit of digging I found solution, you will be probably interested in, here

fig.update_layout(scene_aspectmode='cube')

Full snippet looks like this:

import plotly.graph_objects as go
import numpy as np


x_test_d1 = np.linspace(-4, 4, 100)
x_test_d2 = np.linspace(-3, 3, 100)


grid_d1, grid_d2 = np.meshgrid(x_test_d1, x_test_d2)

z = (
    1.5 * np.sin(grid_d1)
    - 0.1 * (grid_d1 - 3) ** 2
    + 10
    - 0.5 * (grid_d2**2 - 2)
    + np.sin(grid_d2) * 2
)


fig = go.Figure(data=[go.Surface(x=x_test_d1, y=x_test_d2, z=z)])
fig.update_layout(margin=dict(l=20, r=20, t=20, b=20), scene_aspectmode="cube")

fig.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM