我正在尝试将网格添加到使用plotly生成的3D表面。 最终结果应该是这样的:
我当前的代码如下所示:
df_interpolated_annual = ma_df.interpolate(method='linear')
fig_annual = go.Figure(data=[go.Surface(
z=df_interpolated_annual.values,
x=df_interpolated_annual.columns,
y=df_interpolated_annual.index,
colorscale=colorscale
)])
fig_annual.update_layout(title='Annual', scene=dict(
xaxis=dict(title='Variable1', ticktext=df_interpolated_annual.columns.tolist(), tickvals=list(range(len(df_interpolated_annual.columns))), tickfont=dict(size=12)),
yaxis=dict(title='Date', tickfont=dict(size=12)),
zaxis=dict(title='Variable2', tickfont=dict(size=12)),
aspectratio=dict(x=1, y=1, z=0.7)
),
width=1000,
height=1000
)
fig_annual.show()
我尝试使用以下代码添加网格:
fig_annual = go.Figure(data=[go.Surface(
z=df_interpolated_annual.values,
x=df_interpolated_annual.columns,
y=df_interpolated_annual.index,
colorscale=colorscale
)])
x, y = df_interpolated_annual.columns, df_interpolated_annual.index
z = df_interpolated_annual.values
fig_annual.add_trace(go.Mesh3d(
x=x,
y=y,
z=z,
color='red',
opacity=1,
showlegend=False
))
fig_annual.update_layout(title='Annual', scene=dict(
xaxis=dict(title='Variable1', ticktext=x.tolist(), tickvals=list(range(len(x))), tickfont=dict(size=12), showgrid=True),
yaxis=dict(title='Date', tickfont=dict(size=12), showgrid=True),
zaxis=dict(title='Variable2', tickfont=dict(size=12), showgrid=True),
aspectratio=dict(x=1, y=1, z=0.7)
),
width=1000,
height=1000
)
fig_annual.show()
但是,这并没有改变输出。
任何帮助表示赞赏:)
您可以通过创建双向网格并使用参数 go.Scatter3d
用
mode='lines'
绘制线条来创建线框图。然后,您可以使用与在线框图中使用的相同的 x、y 和 z 值来覆盖曲面图。
import numpy as np
import plotly.graph_objs as go
# Creating the data
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 50)
xGrid, yGrid = np.meshgrid(x, y)
z = np.sqrt(xGrid ** 2 + yGrid ** 2)
# Creating the plot
lines = []
line_marker = dict(color='#0066FF', width=2)
for i, j, k in zip(xGrid, yGrid, z):
lines.append(go.Scatter3d(x=i, y=j, z=k, mode='lines', line=line_marker))
lines.append(go.Scatter3d(x=j, y=i, z=k, mode='lines', line=line_marker))
lines.append(
go.Surface(
contours = {
"x": {"show": True, "start": 1.5, "end": 2, "size": 0.04, "color":"white"},
"z": {"show": True, "start": 0.5, "end": 0.8, "size": 0.05}
},
x=x,
y=y,
z=z
))
layout = go.Layout(
title='Mesh + Surface Plot',
scene=dict(
xaxis=dict(
gridcolor='rgb(255, 255, 255)',
zerolinecolor='rgb(255, 255, 255)',
showbackground=True,
backgroundcolor='rgb(230, 230,230)'
),
yaxis=dict(
gridcolor='rgb(255, 255, 255)',
zerolinecolor='rgb(255, 255, 255)',
showbackground=True,
backgroundcolor='rgb(230, 230,230)'
),
zaxis=dict(
gridcolor='rgb(255, 255, 255)',
zerolinecolor='rgb(255, 255, 255)',
showbackground=True,
backgroundcolor='rgb(230, 230,230)'
)
),
showlegend=False,
)
fig = go.Figure(data=lines, layout=layout)
fig.show()