我有两个 3d 图的 python 代码,我希望这两个图在彼此的表面上滑动,并查看它们何时相互重叠、何时存在间隙或何时仅相互接触。为了使两个 3D 绘图一起滑动作为交互式可视化的一部分,我可以使用 Plotly 的动画功能。这将使我能够在不同的情节视图之间顺利过渡。基本上,这是chatgpt的建议,我不知道如何在下面的代码中实现它。
fig = go.Figure(
layout=dict(
template="ggplot2",
margin=dict(l=50, r=10, b=50, t=50),
font=dict(family="Computer Modern", size=12, color="black"),
width=1000,
height=800,
scene=dict(aspectmode="cube"),
legend=dict(
title="", orientation="h", yanchor="bottom", x=0, y=1.0, xanchor="left"
),
)
)
fig.add_trace(
go.Scatter3d(
x=W_vals["x"],
y=W_vals["y"],
z=W_vals["value"],
text=W_vals["value"],
texttemplate="%{text:.2f}",
mode="markers+text",
marker=dict(size=3, color="black"),
name="W Values",
showlegend=True,
)
)
fig.add_trace(go.Surface(
x=W_vals["x"],
y=W_vals["y"],
z=W_vals["value"],))
fig.add_trace(
go.Surface(
x=W_pivot.columns,
y=W_pivot.index,
z=W_pivot.values,
name="W Surface",
opacity=0.8,
showlegend=True,
colorscale="Viridis",
cmid=np.mean(W_pivot.values)
)
)
fig.add_trace(go.Surface(z=data.values, name="Lotus Surface", colorscale="Greys", showlegend=True,visible="legendonly",
))
fig.show()
fig = go.Figure(
layout=dict(
template="ggplot2",
margin=dict(l=50, r=10, b=50, t=50, pad=0),
font=dict(family="Computer Modern", size=12, color="black"),
width=800,
height=800,
scene=dict(aspectmode="cube"),
)
)
fig.add_trace(go.Surface(z=data.values))
fig.update_traces(
contours_z=dict(
show=True, usecolormap=True, highlightcolor="limegreen", project_z=True
)
)
fig.show()
下面是这个项目的代码,我之前发布的代码应该在最后(我没有添加该代码,因为我之前发布过):
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import pickle
# update the model to ensure it is a smooth surface, adjust the obj function
data = pd.read_csv("LotusLeaf3.csv", header=None)
#data = data / 92.808
A = data[0].to_numpy()
B = data[1].to_numpy()
W_vals = pd.read_csv("W_values_E=1_Width=5_Length=5.csv", index_col=0)
W_vals[["variable", "x", "y"]] = W_vals["variable"].str.split("_", expand=True)
W_pivot = W_vals.pivot(index="y", columns="x", values="value")
W_vals.head()
print(W_vals)
z = W_vals["value"].to_numpy()
x = np.unique(W_vals['x'])
x = np.sort(x.astype(int))
y = np.unique(W_vals['y'])
y = np.sort(y.astype(int))
z = z.reshape(np.max(x)+1, np.max(y)+1)
z = z.T
surface_plot = go.Surface(z=z, x=x, y=y)
# scatter_plot = go.Scatter3d(x=x, y=y, z=z, mode='markers', marker=dict(size=5))
fig = go.Figure(data=surface_plot)
fig.update_layout(
autosize=False,
width=800,
height=800,
)
# fig.add_trace(
# go.Scatter3d(
# x=W_vals["x"],
# y=W_vals["y"],
# z=W_vals["value"],
# text=W_vals["value"],
# texttemplate="%{text:.2f}",
# mode="markers+text",
# marker=dict(size=3, color="black"),
# name="W Values",
# showlegend=True,
# )
# )
fig.add_trace(go.Surface(z=W_vals.values, name="Lotus Surface",
colorscale="Greys", showlegend=True,visible="legendonly",))
fig.show()
np.sort(np.unique(W_vals['x']).astype(int))
z.T
fig = go.Figure(
layout=dict(
template="ggplot2",
margin=dict(l=50, r=10, b=50, t=50),
font=dict(family="Computer Modern", size=12, color="black"),
width=1000,
height=800,
scene=dict(aspectmode="cube"),
legend=dict(
title="", orientation="h", yanchor="bottom", x=0, y=1.0,
xanchor="left"
),
)
)
# fig.add_trace(
# go.Scatter3d(
# x=x,
# y=y,
# z=z,
# text=z,
# texttemplate="%{text:.2f}",
# mode="markers+text",
# marker=dict(size=3, color="black"),
# name="W Values",
# showlegend=True,
# )
# )
fig.add_trace(
go.Scatter3d(
x=W_vals["x"],
y=W_vals["y"],
z=W_vals["value"],
text=W_vals["value"],
texttemplate="%{text:.2f}",
mode="markers+text",
marker=dict(size=3, color="black"),
name="W Values",
showlegend=True,
)
)
fig.show()