[英]Plotting a 3D surface plot in Ploty (Python) is stretched / deformed
我正在 Plotly 中繪制一個二維函數。 我正在經歷情節被延伸到高度。 (見下圖)。 在布局中,我已經更改了高度和寬度的值,但對其沒有影響。
那是我的代碼:
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()
這就是結果圖:
感謝您的任何幫助! :)
根據文檔,您可以在此處找到
寬度
代碼: fig.update_layout(width=)
類型:大於等於10的數字
默認值:700
設置繪圖的寬度(以 px 為單位)。
有點違反直覺,設置 webview/窗口圖的大小顯示在中,但不影響圖的大小。
經過一番挖掘,我找到了解決方案,您可能會對這里感興趣
fig.update_layout(scene_aspectmode='cube')
完整片段如下所示:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.