-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_3d.py
More file actions
74 lines (59 loc) · 1.98 KB
/
plot_3d.py
File metadata and controls
74 lines (59 loc) · 1.98 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
"""
3D Plotting
===========
Demonstrate the three 3-D geometry types supported by
:meth:`~anyplotlib.figure_plots.Axes.plot_surface`,
:meth:`~anyplotlib.figure_plots.Axes.scatter3d`, and
:meth:`~anyplotlib.figure_plots.Axes.plot3d`.
Drag to rotate, scroll to zoom, press **R** to reset the view.
"""
import numpy as np
import anyplotlib as vw
# ── Surface ───────────────────────────────────────────────────────────────────
x = np.linspace(-3, 3, 60)
y = np.linspace(-3, 3, 60)
XX, YY = np.meshgrid(x, y)
ZZ = np.sin(np.sqrt(XX ** 2 + YY ** 2))
fig, ax = vw.subplots(1, 1, figsize=(520, 480))
surf = ax.plot_surface(XX, YY, ZZ,
colormap="viridis",
x_label="x", y_label="y", z_label="sin(r)")
fig
# %%
# Scatter plot
# ------------
rng = np.random.default_rng(1)
n = 300
theta = rng.uniform(0, 2 * np.pi, n)
phi = rng.uniform(0, np.pi, n)
r = rng.uniform(0.6, 1.0, n)
xs = r * np.sin(phi) * np.cos(theta)
ys = r * np.sin(phi) * np.sin(theta)
zs = r * np.cos(phi)
fig2, ax2 = vw.subplots(1, 1, figsize=(480, 480))
sc = ax2.scatter3d(xs, ys, zs,
color="#4fc3f7", point_size=3,
x_label="x", y_label="y", z_label="z")
fig2
# %%
# 3-D line — parametric helix
# ----------------------------
t = np.linspace(0, 4 * np.pi, 300)
hx = np.cos(t)
hy = np.sin(t)
hz = t / (4 * np.pi)
fig3, ax3 = vw.subplots(1, 1, figsize=(480, 480))
ln = ax3.plot3d(hx, hy, hz,
color="#ff7043", linewidth=2,
x_label="cos t", y_label="sin t", z_label="t")
fig3
# %%
# Update the surface data live
# ----------------------------
# Call :meth:`~anyplotlib.figure_plots.Plot3D.set_data` to replace the geometry
# without recreating the panel.
ZZ2 = np.cos(np.sqrt(XX ** 2 + YY ** 2))
surf.set_data(XX, YY, ZZ2)
surf.set_colormap("plasma")
surf.set_view(azimuth=30, elevation=40)
fig