-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsurface.py
62 lines (51 loc) · 1.47 KB
/
surface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
import plotly.graph_objs as go
from plotly.subplots import make_subplots
from plotly.io import show
# Functions for surface equations
def surface_equation1(x, y):
return np.sin(np.sqrt(x**2 + y**2))
def surface_equation2(x, y):
return x**2 + y**2
# Create surface plot
def create_surface_plot(eq_index):
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
if eq_index == 1:
z = surface_equation1(x, y)
elif eq_index == 2:
z = surface_equation2(x, y)
return go.Surface(z=z, x=x, y=y, colorscale='Viridis', visible=(eq_index == 1))
# Create subplots
fig = make_subplots(rows=1, cols=1, specs=[[{'type': 'surface'}]])
# Add both surface plots to the figure
fig.add_trace(create_surface_plot(1))
fig.add_trace(create_surface_plot(2))
# Initially, hide the second plot
fig.data[1].visible = False
# Define buttons for toggling between plots
buttons = []
for i in range(1, 3):
buttons.append(dict(
args=[{"visible": [i == j + 1 for j in range(2)]}],
label=f"Equation {i}",
method="update"
))
# Add buttons to layout
fig.update_layout(
updatemenus=[dict(
type="buttons",
direction="right",
buttons=buttons,
pad={"r": 10, "t": 10},
showactive=True,
x=0.1,
xanchor="left",
y=1.1,
yanchor="top"
)],
title_text="3D Surface Plots with Toggle",
scene=dict(aspectmode="cube")
)
show(fig)